再次探究Android ListView缓存机制
概述
虽然现在5.0后Google推出了RecycleView,但在5.0 Lollipop普及前Listview仍会被广泛使用,所以打算再次探究一下Listview的源码,了解一下Listview 的构成及加载机制。
探究
上图简单梳理了Listview的构成及与其相关类之间的关系,并简要地列出了些重要的方法和内部类。
AdapterView
从上图可以清晰的看出Listview归根究底是继承自AdapterView。AdaterView是一个抽象类,一些最基本和通用方法或接口都是在此定义或声明的,其中一些更是开发者所常用的,诸如:
//Item Click 监听接口
/**
* Interface definition for a callback to be invoked when an item in this
* AdapterView has been clicked.
*/
public interface OnItemClickListener {
... ...
void onItemClick(AdapterView<?> parent, View view, int position, long id);
}
//设置Adapter抽象方法
/**
* Sets the adapter that provides the data and the views to represent the data
* in this widget.
*
* @param adapter The adapter to use to create this view's content.
*/
public abstract void setAdapter(T adapter);
此外在AdapterView中实现了DataSetObserver抽象类,我们一般调用mAdapter.notifyChanged()所触发的就是DataSetObserver的onChanged()方法。关键源码如下:
class AdapterDataSetObserver extends DataSetObserver {
private Parcelable mInstanceState = null;
@Override
public void onChanged() {
mDataChanged = true;
mOldItemCount = mItemCount;
mItemCount = getAdapter().getCount();
... ...
}
@Override
public void onInvalidated() {
mDataChanged = true;
... ...
}
... ...
}
AbsListView
AbsListView是继承自AdapterView,在该类中实现了一个非常重要的内部类RecycleBin,内部类RecycleBin其实就是AbsListView缓存机制的核心类,它的作用是管理AbsListView的item存储和取得。AbsListview的缓存分为两级,第一级为activeView,第二级为scrapview。二者的间的转换主要是在layoutChildren()方法进行(该抽象方法在LisView中实现),具体分析见如下源码:
@Override
protected void layoutChildren() {
... ...
//说明RecycleBin并不缓存HeadView和FooterView
// Don't put header or footer views into the Recycler.
//Those are already cached in mHeaderViews;
if (dataChanged) {
//如果data改变了,则当前所有childView都添加至mScrapViews;
for (int i = 0; i < childCount; i++) {
recycleBin.addScrapView(getChildAt(i), firstPosition+i);
if (ViewDebug.TRACE_RECYCLER) {
ViewDebug.trace(getChildAt(i),
ViewDebug.RecyclerTraceType.MOVE_TO_SCRAP_HEAP, index, i);
}
}
} else {
//若data未改变,即第一次加载时,根据当前childCount数量对mArchiveViews赋值。
recycleBin.fillActiveViews(childCount, firstPosition);
}
... ...
switch (mLayoutMode) {
... ...(在switch条件中执行makeAndAddView函数)
}
// Flush any cached views that did not get reused above
//执行makeAndAddView函数后将需要显示的item view已添加至ListView中,
//所以跳出siwtch后会将缓存的mActiveViews全部转换为mScrapViews。
recycleBin.scrapActiveViews();
... ...
}
同时AbsListview中定义了一个ObtainView方法,一般地当Listview加载时若发现没有可复用的itemView时要么从RecycleBin中转换ScrapView,要么是通过mAdapter.getView()获取新的itemView,ObtainView方法就是专门用来处理上述的两种情况,具体分析如下:
View obtainView(int position, boolean[] isScrap) {
... ...
scrapView = mRecycler.getScrapView(position);
View child;
//若scrapView不为空,则将scrapView转换为可复用的itemView
if (scrapView != null) {
... ...
child = mAdapter.getView(position, scrapView, this);
... ...
}else{
//若scrapView为空,则通过adapter.getView()函数获取新的ItemView
child = mAdapter.getView(position, null, this);
... ...
}
}
结语
OK,今天就先总结这么多了,不足之处欢迎指出。当然今后使用RecycleView会是一种趋势,和AS一样,找机会要研究一下。
作者:XycZero
查看原文:http://www.xyczero.com/blog/article/18/.