简体   繁体   English

将图像从 Drawable 添加到 ArrayList 时 RecylerView 滞后

[英]RecylerView Lags when adding Images from Drawable to ArrayList

I am loading images into ArrayList and then setting it to RecylerView with GridLayout.我正在将图像加载到 ArrayList 中,然后使用 GridLayout 将其设置为 RecylerView。 The images are loaded and everything looks good.图像已加载,一切看起来都不错。 But when I scroll the RecylerView it starts lagging.Though images are loaded completely there is a lag while scrolling.但是当我滚动 RecylerView 时它开始滞后。虽然图像已完全加载,但滚动时会出现滞后。

Some answers I came across were about lazy loading the images or using Glide or Picasso libraries.我遇到的一些答案是关于延迟加载图像或使用 Glide 或 Picasso 库。 But how can I implement it?但是我该如何实施呢? Here's how I am adding images.这是我添加图像的方式。

MainActivity:主要活动:

int imageArray[] = new int[]{
        R.drawable.one,
        R.drawable.two, R.drawable.three,
        R.drawable.four,R.drawable.five,...
};
private ArrayList prepareData(){
    ArrayList arrayList = new ArrayList();
    for(int i=0;i<names.length;i++){
        MyModel myModel = new MyModel();
        myModel.setDrawable(imageArray[i]);
        arrayList.add(myModel);
    }
    return arrayList;
}

Here's my Adapter code:这是我的适配器代码:

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.images_card, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {

   viewHolder.tv_logo.setImageResource(mArrayList.get(i).getDrawable());
}

If you are using Glide then use following code to load your drawable image into imageview using Glide如果您使用 Glide,请使用以下代码使用 Glide 将可绘制图像加载到 imageview 中

 Glide.with(context).load(R.drawable.yourImage).into(imageView);

Where "context" is your activity context and "imageView" is your object of ImageView其中“上下文”是您的活动上下文,“imageView”是您的 ImageView 对象

您还可以使用显示图像的网络调用库的本机代码,并且如果存在某种滞后,您可以在图像视图中添加进度条以获得良好的用户体验,并尝试在您的图像视图中使用 AppCompatImageView 而不是 ImageView布局。

It's better to use Glide or Picasso for image loading.最好使用 Glide 或 Picasso 进行图像加载。

Try this尝试这个

Glide.with(yourContext).load(imageArray[position]).into(viewHolder.tv_logo);

Image loading would be smooth and fast.图像加载将是平滑和快速的。

You can try Picasso also.你也可以试试毕加索。

Happy Coding :)快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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