首页 > 解决方案 > Infinite Scroll Recycleview的imageview在滚动时闪烁

问题描述

我正在使用 Google 分页库显示来自服务器的图像。当我显示来自 Localhost 的图像时,它可以完美运行,但使用实时服务器时,它的图像会闪烁。我试图让 localhost 睡眠 2 秒以延迟响应,但 localhost 的图像没有闪烁。我的问题与image-flickering-while-scrolling-in-recyclerview问题相同。但我没有使用 asynctask 所以不能使用它的答案。

Recyclerview 适配器。

public class ItemAdapter extends PagedListAdapter<Item, ItemAdapter.ItemViewHolder> {
private Context mCtx;

ItemAdapter( Context mCtx) {
    super(DIFF_CALLBACK);
    this.mCtx = mCtx;
}

@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_users, parent, false);
    return new ItemViewHolder(view);
}
private static DiffUtil.ItemCallback<Item> DIFF_CALLBACK =
        new DiffUtil.ItemCallback<Item>() {
            @Override
            public boolean areItemsTheSame(Item oldItem, Item newItem) {
                return oldItem.id == newItem.id;
            }

            @Override
            public boolean areContentsTheSame(Item oldItem, Item newItem) {
                return oldItem.equals(newItem);
            }
        };
@Override
public void onBindViewHolder(@NonNull ItemViewHolder viewHolder, int position) {
    Item item = getItem(position);
    final ItemViewHolder holder = (ItemViewHolder) viewHolder;
    holder.image_date.setText(item.getDate());


    GlideApp
            .with(mCtx)
            .asBitmap()
            .load(item.getThumb())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(holder.imageview);
}


class ItemViewHolder extends RecyclerView.ViewHolder {

    TextView image_date, image_weight, imageHit;
    ImageView imageView, imageView2;
    ProgressBar progressBar;

    ItemViewHolder(View view) {
        super(view);

        image_date = itemView.findViewById(R.id.image_date);
        image_weight = itemView.findViewById(R.id.image_weight);
        imageHit = itemView.findViewById(R.id.hit);
        imageView = (ImageView) itemView.findViewById(R.id.thumb_image);
       imageView2 = itemView.findViewById(R.id.thumb_image2);
        progressBar = itemView.findViewById(R.id.progressBarThumb);
    }
}
}

我正在使用这种方法。https://www.simplifiedcoding.net/android-paging-library-tutorial。当我使用与此方法相同的代码时。它工作正常。当我更改代码作为我的使用时,就会出现问题。PS-问题是加载数据时。加载数据后,它可以正常工作。

标签: androidandroid-recyclerviewandroid-paging

解决方案


而不是滑行使用此代码,然后再试一次

  Picasso.with(context) .load(url) .resize(50, 50) // Your preferred size 
  .centerCrop() 
  .into(imageView);

推荐阅读