Android ListView使用Holder优化原理



一  ListView使用Holder优化

在adapter中覆写getView方法,例如:
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ViewHolder viewHolder = null;
			
			if (convertView == null) {
				viewHolder = new ViewHolder();
				
				convertView = mInflater.inflate(R.layout.listview_item, null);
				viewHolder.itemTextView = (TextView) convertView.findViewById(R.id.item_textview);
				viewHolder.itemImageView = (ImageView) convertView.findViewById(R.id.item_imageview);
				
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			String value = String.valueOf( getItem(position) );
			viewHolder.itemTextView.setText( value );
			
			viewHolder.itemImageView.setImageResource(R.drawable.ic_launcher);
			
			return convertView;
		}



其中converView会反复使用,目的是为了提高ListView效率。但是也会造成异步加载数据后,绑定到相应view会出现item显示顺序混乱问题。

疑问?
从代码分析converView是否复用? 如果是,又怎么复用的?
     如果复用猜测肯定用一个数据结构类似堆栈‘先入后出’保存这些View,需要反复使用的时候在依次取出当前没有使用的View


二  分析源码,解决疑问
1. adapter是通过setAdapter与ListView绑定到一起的,先来看ListView.setAdapter部分源码
     public void setAdapter(ListAdapter adapter) {
        ......

        if (mHeaderViewInfos .size() > 0|| mFooterViewInfos.size() > 0) {
            mAdapter = new HeaderViewListAdapter(mHeaderViewInfos , mFooterViewInfos , adapter);
        } else {
            mAdapter = adapter;
        }
	 ......
     }



   发现ListView中把传入的adapter赋值给了ListView的全局变量mAdapter。

     这里用的是适配器模式,View具体如何创建由传入的Adapter决定,Adapter在getView中创建新的View。
     ListView想要创建view肯定是调用了mAdapter.getView方法,但在ListView中全局搜索没找到。那去ListView的父类中找,其父类是AbsListView,一样搜索'mAdapter.getView',在AbsListView.obtainView()方法中查找到,查看下obtainView代码:

2. AbsListView.obtainView部分源码:
    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);
            ......
	}
	......
    }


   

    发现getView的第二个参数,即Adapter.getView(  int    position, View convertView, ViewGroup parent )中的converView的由来,原来是通过  mRecycler   .getScrapView(position) 获取的, 那继续跟下去全局变量mRecycler

3. mRecycler定义
    /**
     * The data set used to store unused views that should be reused during the next layout
     * to avoid creating new ones
     */
    final RecycleBin mRecycler = new RecycleBin();



4. 继续查下RecycleBin源码,这个类是AbsListView的内部类,2中使用的  mRecycler    .getScrapView(position)  部分源码如下:
    class RecycleBin {
	......

        /**
         * Views that were on screen at the start of layout. This array is populated at the start of
         * layout, and at the end of layout all view in mActiveViews are moved to mScrapViews.
         * Views in mActiveViews represent a contiguous range of Views, with position of the first
         * view store in mFirstActivePosition.
         */
        private View[] mActiveViews = new View[0];

        /**
         * Unsorted views that can be used by the adapter as a convert view.
         */
        private ArrayList<View>[] mScrapViews;

        private int mViewTypeCount;

        private ArrayList<View> mCurrentScrap;

	......

        /**
         * @return A view from the ScrapViews collection. These are unordered.
         */
        View getScrapView(int position) {
            ArrayList<View> scrapViews;
            if (mViewTypeCount == 1) {
                scrapViews = mCurrentScrap;
                int size = scrapViews.size();
                if (size > 0) {
                    return scrapViews.remove(size - 1);
                } else {
                    return null;
                }
            } else {
                int whichScrap = mAdapter.getItemViewType(position);
                if (whichScrap >= 0 && whichScrap < mScrapViews.length) {
                    scrapViews = mScrapViews[whichScrap];
                    int size = scrapViews.size();
                    if (size > 0) {
                        return scrapViews.remove(size - 1);
                    }
                }
            }
            return null;
        }
	
	......
    }




5 分析RecycleBin使用的数据结构
一共有两个存储结构分别是ActiveViews 和 ScrapViews
ActiveViews储存当前在界面(手机显示区域)中显示View,移出界面会存入ScrapViews
ScrapViews存储当前已经滑动出当前界面(手机显示区域)显示的View,这些view存储起来相当于回收,当再次请求的时候从此存储中取出反复使用


三 结论
1. ListView通过Adapter.getView的第二个参数,可以达到反复利用已经创建的View以提高效率。
2. 反复使用的view由AbsListView的内部类 RecycleBin管理,通过调用adapter.getView的第二个参数供开发者使用


四 扩展
《Android 自己动手写ListView学习其原理 3 ItemClick,ItemLongClick,View复用》 原理与ListView相同不过是个简化版


转载请注明出处:http://blog.csdn.net/love_world_/article/details/8112827


2013-03-31 更新与自己动手例子连接







你可能感兴趣的:(Android ListView使用Holder优化原理)