android - 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-问题是加载数据时。加载数据后,它可以正常工作。
解决方案
而不是滑行使用此代码,然后再试一次
Picasso.with(context) .load(url) .resize(50, 50) // Your preferred size
.centerCrop()
.into(imageView);
推荐阅读
- javascript - 找到点在直线上的投影
- java - 我的配置文件应该放在哪里以避免 ubuntu 运行和自动运行问题
- javascript - 自动完成搜索中的建议框不出现
- laravel - 无法创建文件到 Stripe。获取错误仅支持纯文件资源流
- javascript - JS草案,提到插件呈现为纯文本
- hive - 重命名 hive ORC 表中的列名会导致新列中的 NULL 值
- time-series - 无监督学习中的时间相关异常检测
- visual-studio - Vs Code,为什么 Prettier 不格式化代码
- c# - Masstransit AddMediator() 方法不存在
- react-native - 无法加载 URL:此 URL 的域未包含在 react-native 中的应用程序域中