ListView source code analysis, listview source code
1. UML diagram
Scrollview is drawn separately to show that the ViewGroup does not have a built-in recycle mechanism. If Scrollview displays a large number of views, You need to manually process them.
2. division of labor in the inheritance system
(1) AdapterView
An AdapterView is a view whose children aredetermined by an {@ link Adapter }.
A. Abstract functions related to the adapter: getAdapter and setAdapter
B. mEmptyView
C. Observer Mode
D. Accessibility: Android provides the Accessibility function and service to help these users operate devices more easily, including text-to-speech, tactile feedback, gesture operations, trackball and handle operations.
(2) AbsListView
Base class that can be used to implementalized lists of items. A list does not have a spatial definition here. forinstance, subclases of this class can display the content of the list in agrid, in a specified usel, as stack, etc.
1. Has a RecycleBin class, which is responsible for generating and recycling views without Spatial positioning information of subviews.
2. touch orfling-based scrolling)
(3) ListView
A view that shows items in a verticallyscrolling list. The items come from the {@ link ListAdapter} associated withthis view.
Display data in a vertical list, arrange and shift specific control list items
Header and footer
3. Advantages: Clear layers and easy scalability
RecycleBin class (implementing the recycle mechanism)
1. member variables
(1) mFirstActivePosition
The position of the first view stored inmActiveViews.
The location of the first view stored in mActiveViews, that is, a visible subview.
(2) mActiveViews
View []
Views that were on screen at the start oflayout. this array is populated at the start of layout, and at the end oflayout all view in mActiveViews are moved to mScrapViews. views in mActiveViewsrepresent a contiguous range of Views, with position of the first view store in
MFirstActivePosition.
View displayed on the screen when the layout starts. This array is filled at the beginning of the layout. After the layout ends, all views are moved to mScrapViews.
(3) mScrapViews
ArrayList <View> []
Unsorted views that can be used by theadapter as a convert view.
The unordered view array can be used by the adapter as the convert view. This ArrayList is the source of the convertView parameter in the getView method of the adapter. Note: Here is an array, because if there are multiple types of data in the adapter, there will be multiple ScrapViews.
2. member functions
(1) shouldRecycleViewType
Determine whether the viewType of the given view can be recycled.
ViewType <0 can be recycled. The ignored items (ITEM_VIEW_TYPE_IGNORE =-1) or HeaderView/FootView (ITEM_VIEW_TYPE_HEADER_OR_FOOTER =-2) are not recycled. If you have special requirements, you can set the custom viewType to-1. Otherwise, memory will be wasted, resulting in OOM.
(2) retrieveFromScrap
A. In the third case, this is the simplest:
At first, after the listview is stable, there is no cached view in the mScrapView. When we scroll up for a short distance (the first part is still displayed at this time), the new view will be displayed, in this case, the listview will call the Adapter. getView, but the cache does not exist. Therefore, convertView is null. Therefore, we need to allocate a block of memory to create a new convertView;
B. Case 2:
In a, we continue to scroll up and directly remove the first view from the screen (assuming there is no new item). In this case, the first view will be detach, and added to the mScrapView. Then, we continue to scroll up and the new item view will be displayed later. At this time, the system will find the View corresponding to position from the mScrapView, obviously, it cannot be found. The last cached view is retrieved from the mScrapView and passed to convertView;
C. First case:
LayoutParams in AbsListView inherits the LayoutParams of ViewGroup. One of the added member variables is scrappedFromPosition. This variable is the position value assigned by addScrapView, that is, the position of the view to be recycled at that time. Therefore, the scrappedFromPosition = position judgment is that if the view at a position is recycled and the position is re-displayed, the original view is displayed first, that is, try to keep the original view unchanged. Of course, if the original view has been used in another location, there is no way.
(3) pruneScrapViews
Make sure that the number of mScrapViews does not exceed the number of mActiveViews (This can happen if an adapter does not recycle its views ). The size of each ScrapView array in the mScrapView should not exceed the size of mActiveView. If the size exceeds, the system considers that the program does not reuse convertView, but creates a new view each time, in order to avoid the risk of generating a large amount of idle memory and increasing OOM, the system will check after each recycle, release the excess part, saving the memory and reducing the OOM risk.
Generate a new sub-view, that is, call mAdapter. getView
1. private void scrollListItemsBy (int amount)
Call scenario: move items one by onKeyDown, onKeyUp, and onKeyMultiple
CommonKey --> arrowScrollImpl --> scrollListItemsBy
(1) initialization:
Here, offsetChildrenTopAndBottom is used to move all sub-views to the specified pixel:
Finally, the mDisplayList offsetTopBottom is called.
(2) Branch sliding up:
(3)
2. Slide the list to generate a list item View
(1) Call Stack when sliding
OnTouchEvent (AbsListView) à scrollIfNeeded (AbsListView)
Or FlingRunnable (AbsListView)
À
TrackMotionScroll (AbsListView)
À
FillGap
(This is an abstract function in AbsListView, which is implemented in ListView; call fillDown to call fillUp up)
(OffsetChildrenTopAndBottom is called before fillGap is called)
À
MakeAndAddView (ListView)
No data changes: get a visual view from the mRecycler
Data changes: obtainView --> setupChild
(2) Key Analysis:
1) obtainView: generate a view, recycle the old view, and call mApapter. getView (AbsListView)
2) The getScrapView In the first line calls the second part to analyze the retrieveFromScrap of the RecycleBin class.
3) setupChild: add the view to the viewgroup to locate the child view (ListView)
Key steps:
A. attachViewToParent or addViewInLayout
B. child. measure (childWidthSpec, childHeightSpec );
C. child. layout (childrenLeft, childTop, childRight, childBottom );
3. Recycle again
If the RecyclerListener interface is implemented, the onMovedToScrapHeap (View) method in the RecyclerListener is called when a view is recycled to the mScrapViews array of RecycleBin due to the sliding of the ListView. RecycleBin is equivalent to a temporary storage of the Views that do not need to be displayed. As the list slides, when these Views need to be displayed, they are taken out of RecycleBin, recycleBin does not recycle objects in mScrapViews.
Therefore, in the project, add the RecyclerListener interface for the ListView, and release the Bitmap resources contained in the ListItem in the onMovedToScrapHeap method, which can greatly reduce memory usage.
Note the RecyclerListener interface:
Sets the recycler listener to be notifiedwhenever a View is set aside in the recycler for later reuse. This listener canbe used to free resources associated to the View.