[Android] Perfect solution to the ListView load network picture scrambling problem

Source: Internet
Author: User

Why

First of all, why do you see a picture jump?

Using Convertview to optimize each item in the ListView, the reuse of item can effectively reduce the memory footprint and make the ListView slide smoother. There is a problem, however, when the top item slides out of the screen, it becomes the next item that will slide in from the bottom. Every time you slip in the item, ask for a picture on the web.

When the first item slips out, it's taken with the picture. Its imageview points to a piece of memory. When it slowly slides out and slides slowly from the bottom, the item at the bottom and the top (only half the screen can see) or point to

Same piece of memory. The same picture is loaded.

But at the bottom of the item as soon as a slide in, began to request the network, to get pictures. When the bottom item gets the picture successfully will overwrite the original picture.

When each item is such an execution logic, the whole item becomes messy and the image is flashing.


How to Solve

There are two ways to solve this problem:

One is to check if the ImageView is empty if not empty (with pictures) when the item is reused.

ViewHolder.imageview.setVisiable (View.gone);

Then this item will continue to execute the request network picture when the requested picture is not empty (the request succeeds and the setting succeeds), at this time in viewHolder.imageview.setVisiable (view.visiable);


The following is the detailed process of the second idea.

Detailed Procedures

Layout file: Only one ListView

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <listview        android:id= "@+id/lv"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"/></relativelayout>

ListView per item layout

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >        <imageview         android:id= "@+id/item_iv"        android:layout_width= "100DP"        android:layout_height= "100DP"        android:layout_gravity= "center_horizontal"        android:src= "@drawable/ Ic_launcher "/></linearlayout>

The point has come, myadapter in the GetView () method, there are two points.

Public Myadapter (Context context,list<info> data) {This.context = Context;this.data = data;} @Overridepublic view GetView (int position, view Convertview, final ViewGroup parent) {final Viewholder holder;if (CONVERTV Iew ==null) {...} Else{...} Get the Entity class object in the collection final info info = data.get (position);//Get the image URL final String img = Info.getimgurl (); <span style= "color:# ff0000; " >//Focus 1, for each imageview set a tag, the value of the image URL (to ensure the uniqueness of the tag). </span>holder.iv.settag (Info.getimgurl ()); Bitmap Bitmap =/* Network request Bitmap*/<span style= "color: #ff0000;" >//Focus 2, get the value of the tag, compare it to the URL of the scaled picture in the item </span>string tag = (String) holder.iv.getTag (); <span style= "color:# ff0000; " >//if the value of this imageview is the same as the address value of the picture that he should put, it means that the picture belongs to this imageview and can be loaded. </span>if (Tag!=null&&tag.equals (Info.getimg ())) {Iv.setimagebitmap (bitmap);} return Convertview;} Class Viewholder{textview Tv;imageview IV;}





Original address: http://blog.csdn.net/bless2015/article/details/46445325

[Android] Perfect solution to the ListView load network picture scrambling problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.