6 RecycleBin解析
RecycleBin是AbsListView的内部类, 是ListView能够实现成百上千条数据都不会OOM最重要的一个原因. 主要变量如下,
private View[] mActiveViews = new View[0]; // 保存listview中可见的元素(view)
private ArrayList<View>[] mScrapViews; // ArrayList组
private int mViewTypeCount;
private ArrayList<View> mCurrentScrap; //
// 当有某个View确定要废弃掉的时候(比如滚动出了屏幕), mScrapViews或者mCurrentScrap就会缓存
// 该view,当mViewTypeCount大于1时,缓存在mCurrentScrap中,否则缓存在mScrapViews中。
// BaseAdapter中的mViewTypeCount为1,其实就是一个分类
private ArrayList<View> mSkippedScrap;
private SparseArray<View> mTransientStateViews; // 缓存短暂的view
1, fillActiveViews
fillActiveViews() 这个方法接收两个参数,第一个参数表示要存储的view的数量,第二个参数表示ListView中第一个可见元素的position值。RecycleBin当中使用mActiveViews这个数组来存储View,调用这个方法后就会根据传入的参数来将ListView中的指定元素存储到mActiveViews数组当中。
void fillActiveViews(int childCount, int firstActivePosition) {
if (mActiveViews.length < childCount) {
mActiveViews = new View[childCount];
}
mFirstActivePosition = firstActivePosition;
//noinspection MismatchedReadAndWriteOfArray
final View[] activeViews = mActiveViews;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
AbsListView.LayoutParams lp = (AbsListView.LayoutParams) child.getLayoutParams();
// Don't put header or footer views into the scrap heap
if (lp != null && lp.viewType != ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
// Note: We do place AdapterView.ITEM_VIEW_TYPE_IGNORE in active views.
// However, we will NOT place them into scrap views.
activeViews[i] = child;
// Remember the position so that setupChild() doesn't reset state.
lp.scrappedFromPosition = firstActivePosition + i;
}
}
}
2, getActiveView
getActiveView() 这个方法和fillActiveViews()是对应的,用于从mActiveViews数组当中获取数据,返回之后activeViews对应位置就为null,所以mActiveViews不能被重复利用。
View getActiveView(int position) {
int index = position - mFirstActivePosition;
final View[] activeViews = mActiveViews;
if (index >=0 && index < activeViews.length) {
final View match = activeViews[index];
activeViews[index] = null;
return match;
}
return null;
}
3, addScrapView
addScrapView() 用于将一个废弃的View进行缓存,该方法接收一个View参数,当有某个View确定要废弃掉的时候(比如滚动出了屏幕),就应该调用这个方法来对View进行缓存,RecycleBin当中使用mScrapViews或者mCurrentScrap这两个List来存储废弃View。
void addScrapView(View scrap, int position) {
•••
if (mViewTypeCount == 1) {
mCurrentScrap.add(scrap);
} else {
mScrapViews[viewType].add(scrap);
}
•••
}
4, getScrapView
和addScrapView的作用刚好相反
View getScrapView(int position) {
final int whichScrap = mAdapter.getItemViewType(position);
if (whichScrap < 0) {
return null;
}
if (mViewTypeCount == 1) {
return retrieveFromScrap(mCurrentScrap, position);
} else if (whichScrap < mScrapViews.length) {
return retrieveFromScrap(mScrapViews[whichScrap], position);
}
return null;
}