Some suggestions for optimization of RecyclerView

RecyclerView has almost replaced ListView & GridView, but in order to achieve a better performance experience, some appropriate optimizations still need to be done.

Layout optimization

Bearing the brunt, reducing the hierarchical structure and reducing the transition rendering can improve the efficiency of item analysis, measurement and rendering. In addition, you should try to avoid multiple measure & layout times.

1. The three major processes of View
Start from the ViewRoot performTraversals()method , go through the three processes in turn measure(), and finally draw a View.layout()draw()

2. ViewGroup performance
In contrast, the measure of FrameLayout & LinearLayout is far better than RelativeLayout, because RelativeLayout measures the child View twice to determine the width and height, because the child Views are dependent on each other. However, if the child View of LinearLayout is added with the attribute of layout_weight, it also needs to be measured twice.

Cancel the default animation

Cancelling the default animation of RecyclerView can also improve some.

mRecyclerView.setItemAnimator(null);

Item Contour

If possible, fix the height of all Items to reduce the number of measurements, especially for GridLayoutManager.

mRecyclerView.setHasFixedSize(true);

RecycledViewPool

You can set a ViewHolder object pool for RecyclerView. This pool is called RecycledViewPool. This object pool can save you the overhead of creating ViewHolder and avoid GC. One is automatically created by default.

RecycledViewPool mPool = mRecyclerView.getRecycledViewPool();

RecycledViewPool can autonomously control the number of ViewHolders that need to be cached:

mPool.setMaxRecycledViews(itemViewType, number);

RecyclerView can set the number of ViewHolders it needs:

mRecyclerView.setItemViewCacheSize(10);

 
Then, you can adjust the draw cache as needed

mRecyclerView.setItemViewCacheSize(20);
mRecyclerView.setDrawingCacheEnabled(true);
mRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

Avoid creating too many objects

Both onCreateViewHolder and onBindViewHolder are time-sensitive, try to avoid cumbersome operations and loop creation of objects. For example, to create an OnClickListener, you can create one globally, and then carry the data through itemView.setTag.

partial refresh

You can use the following methods to replace notifyDataSetChanged to achieve the purpose of partial refresh.

notifyItemChanged(int position)
notifyItemInserted(int position)
notifyItemRemoved(int position)
notifyItemMoved(int fromPosition, int toPosition) 
notifyItemRangeChanged(int positionStart, int itemCount)
notifyItemRangeInserted(int positionStart, int itemCount) 
notifyItemRangeRemoved(int positionStart, int itemCount) 

If you must use notifyDataSetChanged(), then it is better to setmAdapter.setHasStableIds(true)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324809840&siteId=291194637