The situation is the following, there is a RecyclerView load data from sqlite 80 records, item looks like this, on the left is an image, in the center is the text. The problem is that during fast scrolling, the recycler begins to lag tightly. Initially I thought that the problem is in setImageResource() , since I read somewhere that this method is best used for lazy operations. Replaced it with setImageDrawable(drawable) , rendered this piece of code in the Handler

  int imageId = context.getResources().getIdentifier(item.getImageName(), "drawable", context.getPackageName()); Drawable drawable = context.getDrawable(imageId); holder.imageView.setImageDrawable(drawable); 

The situation has not changed. The images are in drawable format VectorDrawable and with a size of 45dp x 45dp. How to optimize recycler , that would not lag with fast scrolling?

UPDATE:

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private Context context; private List<Item> dataList; public MyAdapter(Context context, List<Item> dataList) { this.context = context; this.dataList = dataList; } public class MyAdapter.ViewHolder extends RecyclerView.ViewHolder{ public TextView mTextView; public ImageView imageView; public ViewHolder(View view) { super(view); mTextView = view.findViewById(R.id.name_item); imageView = view.findViewById(R.id.image_item); } } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_item, parent, false); MyAdapter.ViewHoldervh = MyAdapter.ViewHolder(itemView); return vh; } @Override public void onBindViewHolder(final MyAdapter.ViewHolder holder, final int position) { final Item item = dataList.get(position); holder.mTextView.setText(item.getName()); int imageId = context.getResources().getIdentifier(item.getimageId(), "drawable", context.getPackageName()); Drawable drawable = context.getDrawableimageId holder.imageView.setImageDrawable(drawable); } @Override public int getItemCount() { return dataList.size(); } 

}

  • the code of the whole adapter doesn’t hurt, guessing who’s not so long and without result - pavlofff
  • In the adapter, everything is standard, did not see the point of spreading it. TextView and ImageView in ViewHolder, and onBindViewHolder are set values. I dropped the image installation code. Code for TextView, textView.setText (item.getName); That's all - Colibri
  • maybe worth a try glide or picasso - Shevchyk Vitalii
  • I tried picasso, alas, the same thing. - Colibri
  • Do you have Instant Run enabled? If this option is enabled, then even without using it, it will lead to lags in the entire application, including in the lists. Try to turn off the Instant Run and compare the speed - P. Ilyin

3 answers 3

Solved a problem. Recycler slowed down due to complex svg images, there was too much path in them. I had to switch to png with threading for different screens.

    I recommend checking with the help of the logs whether you have reused Holder 's. Perhaps, at the first binding you use a cycle in which you create new Holder 's. Or, for example, you have a RecyclerView inside ScrollView .

    Alas, without the code, really nothing is clear. After all, it’s definitely not a matter of the assignment method .

    • I threw out the adapter code. Recycler in ScrollView is not - Colibri

    Caching for recyclerView solves problems. Recycler unloads the memory of invisible elements, downloads as it appears on the screen. When caching, all the Recyclera items will be in memory, don't forget about it.

    With the parameters you need to play.

     ... recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 0); recyclerView.setItemViewCacheSize(50); recyclerView.setDrawingCacheEnabled(true);