Android: image in listview flickers while calling notifyDataSetChanged on some devices -


as in title, on devices images flicker while notifydatasetchanged being called.

here getview method subclass of baseadapter:

@override public view getview(int position, view convertview, viewgroup arg2) {     string infservice = context.layout_inflater_service;     layoutinflater li = (layoutinflater)context.getsystemservice(infservice);     view v = arg1;     final viewholder holder;      if(v == null)     {         v = li.inflate(r.layout.row_history_settings, arg2, false);         holder = new viewholder();          holder.flag = (imageview) v.findviewbyid(r.id.row_history_settings_image_flag);         holder.currency = (textview) v.findviewbyid(r.id.row_history_settings_text_currency);         holder.tb = (togglebutton) v.findviewbyid(r.id.row_history_settings_tb);         v.settag(holder);     }     else     {         holder = (viewholder) v.gettag();     }      account account = accounts.get(arg0);      int resid = enabled == true ? utils.getflagresidforcurrency(account.currency()) : utils.getflaginactiveresidforcurrency(account.currency());      if(resid != holder.resid)     {            holder.resid = resid;         holder.flag.settag(resid);         new asyncimageloader(holder.flag, context).execute();     }      holder.currency.settext(account.currency());     if(currency!=null)     {         holder.tb.setchecked(currency.equals(account.currency()));         holder.tb.setenabled(true);     }     else     {         holder.tb.setchecked(false);         holder.tb.setenabled(false);     }      holder.tb.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             currency = holder.currency.gettext().tostring();             notifydatasetchanged();         }     });      utils.enabledisableview(v, enabled);      return v; } 

the holder.flag image flickers vary short period every time click togglebutton. appear happen on devices sony xperia u or htc desire z.

here row layout:

<?xml version="1.0" encoding="utf-8"?>  <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="@dimen/row_list_standard_height"     android:background="@drawable/list_item_background"     android:gravity="center_vertical"     android:orientation="horizontal" >      <imageview         android:id="@+id/row_history_settings_image_flag"         android:layout_width="@dimen/row_list_flag_width"         android:layout_height="wrap_content"         android:layout_centervertical="true"         android:layout_marginleft="10dp"         android:adjustviewbounds="true"         android:contentdescription="@string/none"         android:src="@drawable/flag_pln" />      <com.aliorbank.components.customfont.customfonttextview         android:id="@+id/row_history_settings_text_currency"         style="@style/textviewuniversalrobotolightblack"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centervertical="true"         android:layout_gravity="left|center_vertical"         android:layout_marginleft="20dp"         android:layout_torightof="@id/row_history_settings_image_flag"         android:text="@string/universal_currency_pln"         android:textsize="@dimen/font_size_big" />      <togglebutton         android:id="@+id/row_history_settings_tb"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentright="true"         android:layout_centervertical="true"         android:layout_marginright="10dp"         android:background="@drawable/btn_radio_standard"         android:checked="true"         android:text="@string/none"         android:textoff="@string/none"         android:texton="@string/none" /> </relativelayout> 

and asyncimageloader class:

public class asyncimageloader extends asynctask<object, void, bitmap> { private final weakreference<imageview> imageviewreference; private context context; private int resid;  public asyncimageloader(imageview imv, context context) {     imageviewreference = new weakreference<imageview>(imv);     this.context = context;     this.resid = (integer)imv.gettag(); }  @override protected bitmap doinbackground(object... arg0) {     return bitmapfactory.decoderesource(context.getresources(), resid); }  @override protected void onpostexecute(bitmap result) {      if (imageviewreference != null && result != null) {         final imageview imageview = imageviewreference.get();         if (imageview != null) {             imageview.setimagebitmap(result);         }     } }} 

it seems me, convertview not correspond position in getview method. there way convertview corresponds listview's row position? or maybe there solution?

you should cancel asyncimageloader view has been recycled.

what trying achieve really difficult do. can find detailed example here : http://developer.android.com/training/displaying-bitmaps/display-bitmap.html.

a option, except if want have achieved in life, use library.

here options :

i believe common's ware lib well. , sure there other options.

ps : must prefer second option, contributor robospice may not fair ;)


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -