我正在使用显示联系人列表(姓名+图片)ListView.为了使初始加载速度快,我只首先加载名称,然后推迟图片加载.现在,每当我的后台线程完成加载图片时,它会调度我的适配器notifyDataSetChanged()在UI线程上调用.不幸的是,当发生这种情况时,ListView不会重新呈现(即调用getView())已经在屏幕上的项目.因此,用户不会看到新加载的图片,除非他们滚动并返回到同一组项目,以便视图得到回收.一些相关的代码:
private final Map<Long, Bitmap> avatars = new HashMap<Long, Bitmap>();
// this is called *on the UI thread* by the background thread
@Override
public void onAvatarLoaded(long contactId, Bitmap avatar) {
avatars.put(requestCode, avatar);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// snip...
final Bitmap avatar = avatars.get(contact.id);
if (avatar != null) {
tag.avatar.setImageBitmap(avatar);
tag.avatar.setVisibility(View.VISIBLE);
tag.defaultAvatar.setVisibility(View.GONE);
} else {
tag.avatar.setVisibility(View.GONE);
tag.defaultAvatar.setVisibility(View.VISIBLE);
if (!avatars.containsKey(contact.id)) {
avatars.put(contact.id, null);
// schedule the …Run Code Online (Sandbox Code Playgroud)