Compared with ListView, recyclerView has the biggest advantage of deconstruction, which is more conducive to customization and expansion. Such as the layout of layoutManager, splitter line, can achieve more complex UI requirements.

Recyclerview realized four levels of cache: 1. Attachedscrap screen cache; 2. 2. Cacheviews slides out of the page cache; 3. Cacheextensionview Custom cache; 4RecyclerPool Cache pool cache.

Attachedscrap Used for in-screen item reuse, without bindView and createView. The cacheView caches the last two items that were slid off the screen, so that pages can slide slightly without redoing bindView and CreateView. Cacheextensionview is not implemented by default, and developers should implement it themselves if required (which is generally not required). Recylerpool Cache pool cache, where the item is cleared data, reuse needs to re-bindView.

Reuse. When the user slides recyclerView, 1. The cache system will first obtain itemView from the page according to ID and position, if the acquisition fails; 2. The cache system retrieves an ItemView from the secondary cache cacheView based on its ID and position. If there is an item of the corresponding type in recyclerpool, it will return and call bindView operation to realize reuse; otherwise, it will call Adapter. createView to create an item.

Cache: when the user enters recyclerview page, 1. Attachedscrap screen cache will save items on the screen to achieve level 1 cache. 2. When the user swipes recyclerView, the latest item that slides off the screen will be cached in the secondary cache CacheView (note that it must be completely swiped off the screen). At this time, the cached ItemView is bound to data. 3. When the size of level-2 cache exceeds 2, level-2 cache recyclerpool is triggered. When a third sliding item is inserted into the secondary cache, CacheView takes the oldest item from the stored array, removes its binding data, recyclerpool it according to its ItemType, deletes it, and inserts the new cache item first.

The data structures underlying cacheView and Recyclerpool implement caching:

The underlying cacheView uses an arrayList to hold cached items, up to a maximum of 2. When the number exceeds 2, location 0 is thrown into the four-level cache and deleted, so that location 1 becomes 0 and the new item is added to location 1. This means that new and old data is cached first and then deleted.

The bottom layer of Recyclerpool is Sparesarray, key value is an itemType, value value is an arrayList array, sapresArray can be any length, and the arrayList in value is 5 at most, more than 5 is not accepted. That is, you can have as many itemTypes as you want, but each ItemType can accept up to five caches.

Reuse and cache trigger times are mainly at the time of loading (setAdapter) and sliding.

RecyclerView optimization: 1. In bindView do not do time-consuming operations such as date formatting, because often call; 2. Try to use partial refresh, such as when adding and deleting items, or even refresh a control alone; 3. Change the size of the cache. For example, the level 2 cacheView can be increased a bit to exchange space for efficiency. 4.RecyclerView nested use, if the item is the same can use a cache pool;