如标题所示,在调用 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 图像都会闪烁一段时间。这似乎只发生在某些设备上,例如 Sony 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 吗?或者也许还有另一种解决方案?