【发布时间】:2014-09-28 02:20:50
【问题描述】:
我的 ListView 在每个列表行中使用自定义 SimpleCursorAdapter 和 ImageView。
图像被添加到带有Universal Image Loader library 的ImageViews。当我在ListView 中上下滚动时,图像会被打乱。一些本应为空的行突然保留了另一行的图像。当我上下滚动半分钟时,所有应该为空的行都被其他行的图像填充。
我认为有一个recycling problem 和ImageLoader。我不得不说适配器在 TextViews 上运行良好,这就是为什么我认为问题一定出在 ImageLoader 上。
这是我的代码:
我只在整个应用程序中初始化一次配置。这是肯定的。
这段代码在我的MainActivity:
File cacheDir = StorageUtils.getCacheDirectory(this);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.diskCache(new UnlimitedDiscCache(cacheDir))
.diskCacheSize(20 * 1024 * 1024)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.threadPoolSize(3)
.threadPriority(4)
.build();
ImageLoader.getInstance().init(config);
这是我的适配器(为了更好地概览,我删除了 TextView 相关的东西):
public class MyAdapter extends SimpleCursorAdapter {
ImageLoader imageLoader;
DisplayImageOptions options;
public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.on_loading)
.showImageForEmptyUri(R.drawable.empty_uri)
.showImageOnFail(R.drawable.on_fail)
.resetViewBeforeLoading(true)
.delayBeforeLoading(200)
.cacheInMemory(false)
.cacheOnDisk(true)
.build();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = newView(mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder vH = new ViewHolder();
LinearLayout main = new LinearLayout(context);
ImageView imageView = new ImageView(context);
main.addView(imageView)
vH.imageView = imageView;
main.setTag(vH);
return main;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder vH = (ViewHolder) view.getTag();
imageLoader.displayImage("file://" + cursor.getString(cursor.getColumnIndex(MyDatabase.COLUMN_ONE)), vH.imageView, options);
}
private static class ViewHolder {
ImageView imageView;
}
【问题讨论】:
标签: android listview universal-image-loader