【发布时间】:2020-05-27 18:10:04
【问题描述】:
您好,我有一个 RecyclerView,用于在两列中显示图像。电影的 ArrayList 包含 60 个图像的 URL 字符串。因此图像可以很好地加载到 ImageViews 中。当我向下滚动时,一切正常且流畅,然后我到达底部并向上滚动,有一段时间它滚动正常,但随后它滚动到最顶部,我实际上看到了尝试加载之间的所有照片。我寻找了这个问题,但没有找到这个滚动问题的原因。 但是,如果我的列表中只有 20 项,那么一切都会很好
这是适配器的代码:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private ArrayList<Movie> movies;
private Context context;
private int newWidth;
private int newHeight;
public CustomAdapter(ArrayList<Movie> movies, Context context) {
this.movies = movies;
this.context = context;
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
int orientation = context.getResources().getConfiguration().orientation;
newWidth = screenWidth / (orientation == Configuration.ORIENTATION_LANDSCAPE ? 3 : 2);
newHeight = (int) (((double) newWidth / 185) * 278);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_movie, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Picasso.with(context)
.load(movies.get(position).url)
.resize(newWidth, newHeight)
.into(holder.imageView);
}
@Override
public int getItemCount() {
return movies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
final ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.pic);
}
}
}
谢谢
【问题讨论】:
-
我遇到了同样的问题,但是在向 Picasso 添加标签后它起作用了。尝试这样:Picasso.with(context) .load(IMAGE_BASE + W_185 + movies.get(position).image) .resize(newWidth, newHeight) .tag(context) .into(holder.image);
-
@TdSoft 嗯,保持不变
-
请您尝试不同的库,如 Glide(谷歌也推荐使用 Glide)。请参阅此网址inthecheesefactory.com/blog/…,我认为该帖子中的“磁盘缓存”部分将解决您的问题。
-
@TdSoft 是的,带有 Glide 看起来好多了,还有一些,但看起来像 Image Loader 库是问题所在。谢谢
-
你在 Glide(.diskCacheStrategy(DiskCacheStrategy.ALL)) 中尝试过磁盘缓存策略
标签: android scroll android-recyclerview