Why
First of all, why is there a photo bounce?
Using Convertview to optimize every item in a ListView, the reuse of item can effectively reduce the memory footprint. Makes the ListView slide smoother.
But it will bring up a problem when the top item slides out of the screen. 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. Loaded the same picture.
But the bottom item just slides in and starts asking for the network to get the picture. When the bottom item gets the picture successfully will overwrite the original picture.
When every item is this kind of running logic. The whole item will become very messy, the picture is flashing.
How to Solve
There are two ways to solve a problem:
One is to check if the ImageView is empty on the item, assuming it is not empty (there is a picture). The
ViewHolder.imageview.setVisiable (View.gone);
Then this item will continue to run 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 another way of thinking about the specific process.
Specific Process
Layout file: There is 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:orientation=" 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 key 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, set a tag for each of the ImageView. The value is the image URL (which guarantees the uniqueness of the tag).</span>holder.iv.settag (Info.getimgurl ()); Bitmap Bitmap =/* Network request Bitmap*/<span style= "color: #ff0000;" >//Focus 2. Gets the value of the tag. Compare </span>string tag = (String) holder.iv.getTag () <span style= "color: #ff0000;" With the URL of the scaled image in the item. >//assumes that the value of this imageview is the same as the address value of the image he should place, indicating that the image belongs to this imageview. able to load.
</span>if (Tag!=null&&tag.equals (Info.getimg ())) {Iv.setimagebitmap (bitmap);} return Convertview;} Class Viewholder{textview Tv;imageview IV;}
Reprint Please specify the Source: http://blog.csdn.net/bless2015/article/details/46445325
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
[Android] Perfect solution listview Load Network Picture bounce problem