我确实重构了 ViewHolder 模式ListView
并ArrayAdapter
参考了以下链接:http ://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
我的 ListView 项目具有异步 http-json 操作,可将视图动态添加到每个项目中。但是,我有几个子视图以重复方式附加到其他项目的问题。也许它是由重用视图引起的。但我几乎不对待它。
以下源代码(从上面的链接粘贴的异步加载部分)很难理解,因为该行if (mHolder.position == mPosition)
会给出 always true
,不是吗?
static class ThumbnailTask
是放置在适配器类内部还是适配器类之外?是否static class ViewHolder
也放置在适配器类中(我做了。我可以理解,它ViewHolder
已被每个可见项目所取代)?
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
...
holder.position = position;
new ThumbnailTask(position, holder)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
return convertView;
}
private static class ThumbnailTask extends AsyncTask {
private int mPosition;
private ViewHolder mHolder;
public ThumbnailTask(int position, ViewHolder holder) {
mPosition = position;
mHolder = holder;
}
@Override
protected Cursor doInBackground(Void... arg0) {
// Download bitmap here
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (mHolder.position == mPosition) {
mHolder.thumbnail.setImageBitmap(bitmap);
}
}
}
private static class ViewHolder {
public ImageView thumbnail;
public int position;
}