[转][Android] ListView中getView的原理+如何在ListView中放置多个item
ListView 和 Adapter 的基础
工作原理:
- ListView 针对List中每个item,要求 adapter “给我一个视图” (getView)。
 - 一个新的视图被返回并显示
 
如果我们有上亿个项目要显示怎么办?为每个项目创建一个新视图?NO!这不可能!
实际上Android为你缓存了视图。
Android中有个叫做Recycler的构件,下图是他的工作原理:

- 如果你有10亿个项目(item),其中只有可见的项目存在内存中,其他的在Recycler中。
 - ListView先请求一个type1视图(getView)然后请求其他可见的项目。convertView在getView中是空(null)的。
 - 当item1滚出屏幕,并且一个新的项目从屏幕低端上来时,ListView再请求一个type1视图。convertView此时不是空值了,它的值是item1。你只需设定新的数据然后返回convertView,不必重新创建一个视图。
 
请看下面的示例代码,这里在getView中使用了System.out进行输出
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
 | 
public class MultipleItemsList extends ListActivity {      private MyCustomAdapter mAdapter;      @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mAdapter = new MyCustomAdapter();        for (int i = 0; i < 50; i++) {            mAdapter.addItem("item " + i);        }        setListAdapter(mAdapter);    }      private class MyCustomAdapter extends BaseAdapter {          private ArrayList mData = new ArrayList();        private LayoutInflater mInflater;          public MyCustomAdapter() {            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);        }          public void addItem(final String item) {            mData.add(item);            notifyDataSetChanged();        }          @Override        public int getCount() {            return mData.size();        }          @Override        public String getItem(int position) {            return mData.get(position);        }          @Override        public long getItemId(int position) {            return position;        }          @Override        public View getView(int position, View convertView, ViewGroup parent) {            System.out.println("getView " + position + " " + convertView);            ViewHolder holder = null;            if (convertView == null) {                convertView = mInflater.inflate(R.layout.item1, null);                holder = new ViewHolder();                holder.textView = (TextView)convertView.findViewById(R.id.text);                convertView.setTag(holder);            } else {                holder = (ViewHolder)convertView.getTag();            }            holder.textView.setText(mData.get(position));            return convertView;        }      }      public static class ViewHolder {        public TextView textView;    }} | 
执行程序,然后在Logcat中查看日志

getView 被调用 9 次 ,convertView 对于所有的可见项目是空值(如下)
02-05 13:47:32.559: INFO/System.out(947): getView 0 null02-05 13:47:32.570: INFO/System.out(947): getView 1 null02-05 13:47:32.589: INFO/System.out(947): getView 2 null02-05 13:47:32.599: INFO/System.out(947): getView 3 null02-05 13:47:32.619: INFO/System.out(947): getView 4 null02-05 13:47:32.629: INFO/System.out(947): getView 5 null02-05 13:47:32.708: INFO/System.out(947): getView 6 null02-05 13:47:32.719: INFO/System.out(947): getView 7 null02-05 13:47:32.729: INFO/System.out(947): getView 8 null | 
然后稍微向下滚动List,直到item10出现:

convertView仍然是空值,因为recycler中没有视图(item1的边缘仍然可见,在顶端)
02-05 13:48:25.169: INFO/System.out(947): getView 9 null | 
再滚动List

convertView不是空值了!item1离开屏幕到Recycler中去了,然后item11被创建
02-05 13:48:42.879: INFO/System.out(947): getView 10 android.widget.LinearLayout@437430f8 | 
再滚动:
02-05 14:01:31.069: INFO/System.out(947): getView 11 android.widget.LinearLayout@437447d002-05 14:01:31.142: INFO/System.out(947): getView 12 android.widget.LinearLayout@43744ff802-05 14:01:31.279: INFO/System.out(947): getView 13 android.widget.LinearLayout@43743fa802-05 14:01:31.350: INFO/System.out(947): getView 14 android.widget.LinearLayout@4374582002-05 14:01:31.429: INFO/System.out(947): getView 15 android.widget.LinearLayout@4374604802-05 14:01:31.550: INFO/System.out(947): getView 16 android.widget.LinearLayout@4374687002-05 14:01:31.669: INFO/System.out(947): getView 17 android.widget.LinearLayout@4374709802-05 14:01:31.839: INFO/System.out(947): getView 18 android.widget.LinearLayout@437478c002-05 14:03:30.900: INFO/System.out(947): getView 19 android.widget.LinearLayout@43748df002-05 14:03:32.069: INFO/System.out(947): getView 20 android.widget.LinearLayout@437430f8 | 
convertView 如我们所期待的非空了,在item11离开屏幕之后,它的视图(@437430f8)作为convertView容纳item21了
不同的项目布局(item layout)
我们再举一个稍微复杂的例子,在上例的list中加入一些分隔线
你需要做这些:
- 重(@Override)写 getViewTypeCount() – 返回你有多少个不同的布局
 - 重写 getItemViewType(int) – 由position返回view type id
 - 根据view item的类型,在getView中创建正确的convertView
 
以下是代码:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
 | 
public class MultipleItemsList extends ListActivity {      private MyCustomAdapter mAdapter;      @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mAdapter = new MyCustomAdapter();        for (int i = 1; i < 50; i++) {            mAdapter.addItem("item " + i);            if (i % 4 == 0) {                mAdapter.addSeparatorItem("separator " + i);            }        }        setListAdapter(mAdapter);    }      private class MyCustomAdapter extends BaseAdapter {          private static final int TYPE_ITEM = 0;        private static final int TYPE_SEPARATOR = 1;        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;          private ArrayList mData = new ArrayList();        private LayoutInflater mInflater;          private TreeSet mSeparatorsSet = new TreeSet();          public MyCustomAdapter() {            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);        }          public void addItem(final String item) {            mData.add(item);            notifyDataSetChanged();        }          public void addSeparatorItem(final String item) {            mData.add(item);            // save separator position            mSeparatorsSet.add(mData.size() - 1);            notifyDataSetChanged();        }          @Override        public int getItemViewType(int position) {            return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;        }          @Override        public int getViewTypeCount() {            return TYPE_MAX_COUNT;        }          @Override        public int getCount() {            return mData.size();        }          @Override        public String getItem(int position) {            return mData.get(position);        }          @Override        public long getItemId(int position) {            return position;        }          @Override        public View getView(int position, View convertView, ViewGroup parent) {            ViewHolder holder = null;            int type = getItemViewType(position);            System.out.println("getView " + position + " " + convertView + " type = " + type);            if (convertView == null) {                holder = new ViewHolder();                switch (type) {                    case TYPE_ITEM:                        convertView = mInflater.inflate(R.layout.item1, null);                        holder.textView = (TextView)convertView.findViewById(R.id.text);                        break;                    case TYPE_SEPARATOR:                        convertView = mInflater.inflate(R.layout.item2, null);                        holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);                        break;                }                convertView.setTag(holder);            } else {                holder = (ViewHolder)convertView.getTag();            }            holder.textView.setText(mData.get(position));            return convertView;        }      }      public static class ViewHolder {        public TextView textView;    }} | 
运行程序,你会看到每4个item一个分割线

看看日志,无异常,所有的convertView都是空的
02-05 15:19:03.080: INFO/System.out(1035): getView 0 null type = 002-05 15:19:03.112: INFO/System.out(1035): getView 1 null type = 002-05 15:19:03.130: INFO/System.out(1035): getView 2 null type = 002-05 15:19:03.141: INFO/System.out(1035): getView 3 null type = 002-05 15:19:03.160: INFO/System.out(1035): getView 4 null type = 102-05 15:19:03.170: INFO/System.out(1035): getView 5 null type = 002-05 15:19:03.180: INFO/System.out(1035): getView 6 null type = 002-05 15:19:03.190: INFO/System.out(1035): getView 7 null type = 002-05 15:19:03.210: INFO/System.out(1035): getView 8 null type = 002-05 15:19:03.210: INFO/System.out(1035): getView 9 null type = 1 | 
滚动list:
02-05 15:19:54.160: INFO/System.out(1035): getView 10 null type = 002-05 15:19:57.440: INFO/System.out(1035): getView 11 android.widget.LinearLayout@43744528 type = 002-05 15:20:01.310: INFO/System.out(1035): getView 12 android.widget.LinearLayout@43744eb0 type = 002-05 15:20:01.880: INFO/System.out(1035): getView 13 android.widget.LinearLayout@437456d8 type = 002-05 15:20:02.869: INFO/System.out(1035): getView 14 null type = 102-05 15:20:06.489: INFO/System.out(1035): getView 15 android.widget.LinearLayout@43745f00 type = 002-05 15:20:07.749: INFO/System.out(1035): getView 16 android.widget.LinearLayout@43747170 type = 002-05 15:20:10.250: INFO/System.out(1035): getView 17 android.widget.LinearLayout@43747998 type = 002-05 15:20:11.661: INFO/System.out(1035): getView 18 android.widget.LinearLayout@437481c0 type = 002-05 15:20:13.180: INFO/System.out(1035): getView 19 android.widget.LinearLayout@437468a0 type = 102-05 15:20:16.900: INFO/System.out(1035): getView 20 android.widget.LinearLayout@437489e8 type = 002-05 15:20:25.690: INFO/System.out(1035): getView 21 android.widget.LinearLayout@4374a8d8 type = 0 | 
convertView对于分割线是空的,直到第一个分割线可见,当其离开屏幕,视图去到Recycler并且convertView开始起作用。
本文翻译自http://android.amberfog.com/?p=296
[转][Android] ListView中getView的原理+如何在ListView中放置多个item的更多相关文章
- CursorAdapter中getView newView bindView异同
		
Adapter的作用是界面与数据之间的桥梁,通过设置适配器至ListView控件后(如调用ListView的 setAdapter(ListAdapter adapter) ...
 - 用代码说话:如何在Java中实现线程
		
并发编程是Java语言的重要特性之一,"如何在Java中实现线程"是学习并发编程的入门知识,也是Java工程师面试必备的基础知识.本文从线程说起,然后用代码说明如何在Java中实现 ...
 - 转:ListView中getView的工作原理
		
ListView中getView的工作原理: [1]ListView asks adapter “give me a view” (getView) for each item of the list ...
 - android ListView 中getview学习总结
		
最近在做android相关的开发,ListView中有一个图片错位的问题,今天查了很多人写的一些东西,所以记录下来,算是一种加深理解吧. ListView是一个非常常用的控件,功能可以扩展的很丰富,而 ...
 - Android 如何在 ListView 中更新 ProgressBar 进度
		
=======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...
 - listview中getview异步加载网络图片
		
前言:本以为异步加载挺简单,因为网上代码多,但真想要做好,还真不那么简单,从看代码到弄懂再到自己写,实在是有太多的东西需要学了,用了两天的时间,终于弄出来了,因为用到回调函数,所以理解起来可能难度有点 ...
 - ListView getView中放置多个item和getItemViewType的用法
		
ListView 和 Adapter 的基础 工作原理: ListView 针对List中每个item,要求 adapter “给我一个视图” (getView). 一个新的视图被返回并显示 如果我们 ...
 - Android ListView动画特效实现原理及源代码
		
Android 动画分三种,当中属性动画为我们最经常使用动画,且能满足项目中开发差点儿所有需求,google官方包支持3.0+.我们能够引用三方包nineoldandroids来失陪到低版本号.本样例 ...
 - 关于ListView中getView被重复调用的问题
		
我用ListView显示数据时,自定义了一个适配器(extends ArrayAdapter),然后重写了getView方法,现在出现一个问题,就是这个getView()方法被重复调用了,比如我的_d ...
 
随机推荐
- 如何将vmworkstation的虚机导成ovf模版
			
如何将vmworkstation的虚机导成ovf模版 最近碰见一个事情挺烦的苦恼了我好长一段时间,是这样的公司要进行攻防演练需要搭建一个owaps的靶站练手,环境我在我的电脑上已经搭好了(vmwork ...
 - 35 - 并发编程-GIL-多进程
			
目录 1 GIL 1.1 为什么会有GIL 1.2 GIL与thread lock 1.3 个人总结 2 multiprocessing模块 2.1 Process类 2.2 Process类的方法 ...
 - Tslib触摸屏官网【转】
			
转自:https://github.com/kergoth/tslib C library for filtering touchscreen events tslib consists of the ...
 - 【转载】C#异常Retry通用类
			
//Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...
 - svm和svr区别--摘自其它博客
			
学习笔记:SVM柔性边界的补充和SVR(支持向量回归) 作者 小刺猬yyx 关注 2016.08.06 10:31* 字数 1608 阅读 421评论 0喜欢 2 上一个笔记对于SVM不能完美分类的情 ...
 - java网络编程三次握手四次挥手
			
第一次握手:client设置syn=1,随机产生一个序列号seq=x,将数据包发送到server.client进入syn_send状态, 等待server确认. 第二次握手:server查看clien ...
 - CSS初步了解
			
CSS 概述 个人理解为对html的扩展,对html关键字进行功能添加. CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表 ...
 - 四、ansible主机组定义
			
1.打开hosts文件 vim /etc/ansible/hosts 2.定义一个主机组 [web-server] 192.168.1.1 3.定义多个组(继承) [web:children] web ...
 - Windows内核进程管理器解析
			
Windows内核是如何实现线程挂起的?如何实现线程挂载到进程的?如何实现杀死进程和线程的? 从源码分析一下,这些操作具体在源码上是如何实现的. 进程创建.线程切换.线程跨越CPU权限级.进程挂靠.杀 ...
 - 《HBase权威指南》学习笔记
			
第一章 简介 背景: GFS:集群存储海量数据,数据在节点间冗余复制,即使一台存储服务器发生故障,也不会影响可用性. GFS的缺点:适合存储少许非常大的文件,而不适合存储大量小文件,因为文件的元数据 ...