Comparative Analysis of RecyclerView and ListView: cache mechanism,
I. background
PS: Related Knowledge:
The principle of the ListView and RecyclerView caching mechanisms is roughly similar, as shown in:
During the Sliding Process, the ItemView of the off-screen is recycled to the cache. The ItemView of the incoming screen is obtained from the cache first, but the Implementation Details of ListView and RecyclerView are different. (This is only one of the cache usage scenarios, such as refresh)
Ii. Comparison of the cache mechanism in text 2.1
1. different levels:
RecyclerView has two more levels of caching than ListView. It supports multiple off-ItemView caches, developer-defined cache processing logic, and all recyclerviews share the same RecyclerViewPool ).
Specifically:
ListView (two-level cache ):
RecyclerView (Level 4 cache ):
The cache mechanisms of ListView and RecyclerView are basically the same:
1). the mActiveViews and mAttachedScrap functions are similar, meaning that the ItemView of the list items visible on the screen can be reused quickly without the need to recreate createView and bindView;
2). The mScrapView and mCachedViews + mReyclerViewPool functions in a similar way. It caches the ItemView that leaves the screen, so that the ItemView that is about to enter the screen can be reused.
3 ). recyclerView has the advantage of. the use of mCacheViews allows you to quickly reuse the list item ItemView outside the screen without the need for bindView when entering the screen. B. the mRecyclerPool can be used by multiple recyclerviews. In specific scenarios, such as viewpaper + multiple list pages, it has an advantage. objectively speaking, RecyclerView reinforces and improves the ListView cache mechanism in specific scenarios.
2. Different caches:
1). RecyclerView caches RecyclerView. ViewHolder. The abstract can be understood:
View + ViewHolder (avoid calling findViewById every time you create a View) + flag (identify status );
2). ListView cache View.
The cache is different, and the two are slightly different in the cache usage, specifically:
The process for obtaining the cache from ListView is as follows:
RecyclerView:
1 ). when mCacheViews (out-of-screen) in RecyclerView gets the cache of the target location by matching pos. The advantage of this is that when the data source remains unchanged, you do not need to re-bindView dview:
The same is the off-screen cache. ListView obtains the corresponding cache from mScrapViews Based on pos, but does not directly use it. Instead, it re-getView (that is, it will re-bindView). The related code is as follows:
// AbsListView source code: line2345 // obtain the cache final View scrapView = mRecycler from the mScrapView by matching the pos. getScrapView (position); // call getView directly regardless of whether it is successful or not, which will definitely call createViewfinal View child = mAdapter. getView (position, scrapView, this); if (scrapView! = Null) {if (child! = ScrapView) {mRecycler. addScrapView (scrapView, position);} else {...}}
2) In ListView, view is obtained through pos, that is, pos --> view;
In RecyclerView, viewholder is obtained through pos, that is, pos --> (view, viewHolder, flag );
From the flowchart, we can see that flag is used to determine whether the view needs to be re-bindView, which is also the core of RecyclerView to implement partial refresh.
2.2 partial refresh
As we can see from the above, the cache mechanism of RecyclerView is indeed more complete, but it is not a qualitative change. The major highlight of RecyclerView is that it provides a partial refresh interface through partial refresh, you can avoid calling many useless bindView.
(RecyclerView and ListView add, remove Item Effect Comparison)
Combined with the cache mechanism of RecyclerView, we can see how partial refresh is implemented:
Taking policyitemremoved (1) In RecyclerView as an example, requestLayout () is called to redraw the entire RecyclerView. The process is as follows:
OnMeasure () --> onLayout () --> onDraw ()
OnLayout () focuses on three steps:
DispathLayoutStep1 (): record the information of the ItemView in the list item before refresh, such as Top, Left, Bottom, Right, used for animation-related computation;
DispathLayoutStep2 (): The layout size and position are measured. The core function is layoutChildren ();
DispathLayoutStep3 (): calculates the status of each ItemView before and after the layout, such as Remove, Add, Move, and Update. If necessary, execute the corresponding animation.
LayoutChildren () flowchart:
When policyitemremoved is called, The ItemView in the screen is preprocessed to modify the pos and flag corresponding to the ItemView (the red part in the flowchart ):
When RecyclerView. getViewForPosition (pos) is called in fill (), RecyclerView performs preprocessing on pos and flag so that bindview can be called only once.
It should be pointed out that the biggest difference between ListView and RecyclerView is the processing logic of the cache when the data source changes. ListView is a "one-pot client", which transfers all mActiveViews to the second-level cache mScrapViews, recyclerView makes it more flexible to modify the flag of each View to identify whether to re-bindView dview.
Iii. Conclusion
1. In some scenarios, such as interface initialization and sliding, ListView and RecyclerView can work well. There is no big difference between the two:
At the beginning of the article, this problem is thrown out. In the Android client card coupon module, most UIS are displayed in the form of list pages and implemented as ListView. Is it necessary to replace them with RecyclerView?
The answer is no. In terms of performance, RecyclerView does not significantly improve, and does not need to be updated frequently. animation is not supported for the moment. This means that RecyclerView has no obvious advantages and is not very attractive, listView can meet business needs well.
2. frequent update of data sources, such as bullet screen: http://www.jianshu.com/p/2232a63442d6 and other RecyclerView advantages will be very obvious;
Further, the conclusion is:
The list page display interface must support animations or frequent updates and partial refreshes. We recommend that you use RecyclerView to make it more powerful and scalable. In other cases (such as the package list page), both are OK, however, ListView is more convenient and convenient to use.