简体   繁体   中英

Android: Images in recycler view item shuffle a lot on scrolling fast

I have a recycler view in my app. Each recycler view item has an imageView. I use an asyncTask to load the bitmaps and add them to the disk cache before setting them into the imageview in onPostExecute.

The problem is that if I scroll through the list fast I can see the images shuffling a lot before the correct image is set for that item. What I understand of this behavior is that as I scroll the list the recycler view item content is replaced with the content of the new item. But since images take some time to load this results in the shuffling effect.

My question is that is there any way to prevent this effect or at least minimize it so that very little shuffling occurs?

Edit: I have tried setting image bitmap to null but it doesn't seem to make any difference.

 @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.title.setText(list.get(position).getTitle());
        holder.subTitle.setText(list.get(position).getSub());
        holder.image.setImageBitmap(null);
        loadBitmap(position, holder.image);
    }

Add below line in onBindViewHolder();

holder.setIsRecyclable(false);

You shouldn't use asyncTask to load multiple bitmaps in recycler views. You can either choose to create your own ThreadPool or using 3rd party library like Picasso or Glide. AsyncTask will tried to load the bitmap into your list item that you've referenced, however because you're scrolling too fast it's all messed up.

使用 3rd 方库,如 Universal Image Loader(UIL) 或 Picasso 或 Glide。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM