我们知道ListView通过复用机制使得在使用时不会引起OOM的出现,那么ListView的复用机制到底是如何实现的呢?首先要讲到ListView的核心类RecycleBin.
RecycleBin
RecycleBin是ListView的父类AbsListView的一个类。他有两个数组mActiveViews和mScrapViews.ActiveView就是当前屏幕正在显示的view.而当屏幕滑动时,有些view移出屏幕外面,就会变成scrapView.scrapvView并不是被销毁了,而是用于复用。
ListView复用机制
ListView复用机制最核心的两个函数就是makeAndAddView和obtainView.下面结合ListView的绘制过程来讲解复用机制。
首先第一次绘制时,会通过mRecycler.getActiveView从mActiveViews数组里面去找,这时候画面刚开始加载mActiveViews肯定是空的,所以接着调用obtainView,在obtainView中调用mRecycler.getScrapView,同样mScrapViews这时候也是空的,因此只能调用mAdapter.getView,这就是我们熟悉的getView.由于这时候convertView=null因此就需要inflate view.也就是第一次加载时,所有的view都是需要inflate方法加载出来的。同时只会加载屏幕可见数量的View,比如屏幕可以显示10个item,但是一共有1000个item,只会加载10item.
而当滑动时,比如往上滑动一个item的位置,会将第一个item移入mScrapViews,而2~9的item在mActiveViews已经存在就可以直接复用,而第十个item则需要从mScrapViews获取,因为此时mScrapViews保存之前移除屏幕的那个item.因此在mAdapter.getView中如果convertView!=null就可以直接用这个item,不需要重新通过inflate创建。
20150719213754421.png
private View makeAndAddView(int position, int y, boolean flow, int childrenLeft,
boolean selected) {
if (!mDataChanged) {
final View activeView = mRecycler.getActiveView(position);
if (activeView != null) {
setupChild(activeView, position, y, flow, childrenLeft, selected, true);
return activeView;
}
}
final View child = obtainView(position, mIsScrap);
setupChild(child, position, y, flow, childrenLeft, selected, mIsScrap[0]);
return child;
}
View obtainView(int position, boolean[] isScrap) {
isScrap[0] = false;
View scrapView;
scrapView = mRecycler.getScrapView(position);
View child;
if (scrapView != null) {
child = mAdapter.getView(position, scrapView, this);
if (child != scrapView) {
mRecycler.addScrapView(scrapView);
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
} else {
isScrap[0] = true;
dispatchFinishTemporaryDetach(child);
}
} else {
child = mAdapter.getView(position, null, this);
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
}
return child;
}
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
ListView性能优化
第一种就是上面说到的,当convertView!=null表明mSrapViews里面有可复用的view。
第二种就是使用viewHolder.就算复用了mSrapViews里的view,但是每次绑定数据的时候都需要调用一次findViewById,因此可以通过setTag将包含了所有控件实例的viewHolder存进view,每次要使用时就通过getTag来获取viewHolder。
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);
ViewHolder viewHolder;
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.fruitImage = view.findViewById(R.id.fruit_image);
viewHolder.fruitName = view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}