2013-09-25 29 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

無論如何,它指出我在正確的方向,現在它似乎工作!謝謝! –

相關問題