Three of ali’s four rounds of interviews asked about RecyclerView. Interview points are different, there are principles, nested problems, cache implementation, but in the end are all the same, all the problems are gathered in, how to do RecyclerView performance optimization?

1.1 RecyclerView the first layout, will pre-layout occur?

Pre-layout is not triggered for the first time. Pre-layout will only be triggered each time notify change. The purpose is to record the coordinates of ViewHolder at each position on the screen by saveOldPosition method. After relayout, the animation effect of Item will be realized by comparison. For example:

1.2 What should I pay attention to if I customize LayoutManager?

In the dispatchLayoutStep1 phase of RecyclerView, Will call the custom LayoutManager supportsPredictiveItemAnimations method whether in some state display predictive animation. Implementation of the following LinearLayoutManager:

@Override
public boolean supportsPredictiveItemAnimations(a) {
return mPendingSavedState == null && mLastStackFromEnd == mStackFromEnd;
}
Copy the code

If supportsPredictiveItemAnimations returns true, then in the LayoutManager autotype onLayoutChildren method is called twice: once in the pre – layout, another is real – layout.

Because there will be pre-layout and real-layout, so in the custom LayoutManager, we need to distinguish between the two layouts according to the return value of isPreLayout method in RecyclerView.State. For example, onLayoutChildren in the LinearLayoutManager has the following judgment:

There is a comment in the code above:

if the child is visible and we are going to move it around, we should layout extra items in the opposite direction to make sure new items animate nicely instead of just fading in

It means that if the item currently being updated is visible, an additional item needs to be filled in the pre-layout stage to ensure that the item in the invisible state can slide smoothly onto the screen.

1.3 Examples

For example, in the figure below, click Item2 to remove it. After notifyItemRemoved, Item5 was not added to RecyclerView before pre-Layout, but after pre-Layout, Item5 was added to RecyclerView after layout

When item is removed from the screen, Item5 moves up along with Item3 and Item4, as shown below:

If the custom LayoutManager does not implement pre-layout or does not implement it properly, then when Item2 is removed from the screen, only Item3 and Item4 will be smoothly moved, and Item5 will simply appear on the screen, as shown below:

It can be seen that Item5 does not scroll smoothly into the screen with Item3 and Item4, which would give the user the impression of being stuck.

1.4 When is ViewHolder cached into RecycledViewPool?

There are mainly the following two situations:

  1. When the ItemView is swiped off screen and CachedView is full, the ViewHolder is cached into the RecycledViewPool
  2. When data changes, ViewHolder after disappearRance will be cached in RecycledViewPool

1.5 Relationship between CachedView and RecycledViewPool

When an ItemView is swiped off the screen, it is saved in CachedView by default. CachedView has a default size of 2 and can be changed using the setItemViewCacheSize method. When CachedView is full and a new ItemView slips out of the screen, it will force CachedView to transfer the ViewHolder of cache to RecycledViewPool according to FIFO rules. The effect can be seen in the following figure:

The default size of RecycledViewPool is 5. You can modify the cache size of RecycledViewPool in the following ways:

RecyclerView.getRecycledViewPool().setMaxRecycledViews(int viewType, int max);
Copy the code

1.6 Differences between CachedView and RecycledViewPool

The ViewHolder cached in the CachedView does not clean up relevant information (such as position, state, etc.), so a ViewHolder that has just been removed from the screen can be retrieved and displayed in the CachedView when it is moved back to the screen. No rebinding (bindViewHolder) is required.

And the ViewHolder cached in RecycledViewPool will be cleaned state and location information, so it is necessary to call bindViewHolder binding data again to find the ViewHolder from RecycledViewPool.

1.7 From what aspects did you optimize RecyclerView?

I summarized several points, mainly can optimize RecyclerView from the following aspects:

Try to put complex data processing operations into asynchrony. The data that RecyclerView needs to display is often obtained from the remote server request, but after the network request gets the data, the data needs to be flattened, and the best data format is returned to the UI thread as far as possible.

Optimize the RecyclerView layout to avoid using it with ConstraintLayout

For fast sliding events, you can use addOnScrollListener to add a listener for fast sliding to stop loading data when the user is fast sliding.

If the ItemView has a fixed height, use setHasFixSize(true). In this way, RecyclerView can directly calculate the height in onMeasure stage, without the need to calculate the height of sub-ItemView for many times. This situation is very significant for vertical RecyclerView nested transverse RecyclerView.

When UI is Tab feed stream, consider using RecycledViewPool to implement cache sharing of multiple RecyclerViews.

In addition, I share a collection from the InternetAndroid learning PDF+ architecture video + interview documents, etc, as well asAndroid development interview materials, advanced architecture materialsFor you to learn more,Click to view

If you need it now, you can check out the latest version of Android Development Essentials Notes, and don’t forget to hit Star as you pass by

If you like this post, give me a thumbs-up, leave a comment or share your support

Android Development Core Knowledge Notes