2013-09-25 30 views
4

与标题中一样,在某些设备上调用notifydatasetchanged时,图像会闪烁。Android:listview中的图像闪烁,同时在某些设备上调用notifyDataSetChanged

这里是我的BaseAdapter的子类,我getView方法:

@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; 
} 

的holder.flag图像闪烁每一个我点击切换按钮的时间变化短的时间。这只会出现在像索尼的Xperia U或的HTC Desire Z.

这里有些设备出现的情况是我行布局:

<?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> 

和我AsyncIMageLoader类:

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); 
     } 
    } 
}} 

在我看来, ,convertView不对应于getView方法中的位置。有没有什么办法可以获得对应于listview的行位置的convertView? 或者也许有另一种解决方案?

回答

3

您应该从已回收的视图中取消AsyncImageLoader。

你试图实现的是真的很难做到。你可以在这里找到一个非常详细的例子:http://developer.android.com/training/displaying-bitmaps/display-bitmap.html

一个很好的选择,除非你真的想在你的生活中实现这个目标,那就是使用一个库。

这里有一些选择:

相信常见的洁具。当然还有其他的选择。

PS:我必须说,我更喜欢第二个选项,但作为一个贡献者RoboSpice我可能不会是公平的;)

+0

谢谢您的回答!我试图遵循http://developer.android.com/training/displaying-bitmaps/display-bitmap.html,并简单地将所有必需的方法复制到我的适配器,但是有一个问题'最终AsyncDrawable asyncDrawable = 新的AsyncDrawable (getResources(),mPlaceHolderBitmap,task);'。看来,必须放置一些图像占位符。 –

+0

无论如何,它指出我在正确的方向,现在它似乎工作!谢谢! –

相关问题