标签:android style blog http color java 使用 os
转载自:http://blog.csdn.net/pkxiuluo01/article/details/7380974
以前一直在用BaseAdapter,对于其中的getview方法的重写一直不太清楚。今天终于得以有空来探究它的详细机制。
下面先讲讲我遇到的几个问题:
一.View getview(int position, View convertview, ViewGroup parent )中的第二个参数是什么含义;
二.View的SetTag和getTag方法的用途;
先来解决第一个问题:
android SDK中这样讲参数 convertview :
the old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using.
If it is not possible to convert this view to display the correct data, this method can create a new view.
翻译:
如果可以的话,这是旧View(这里不便翻译有的人翻成视图)的重用。 建议:在用之前,你应该检查这个View是
不是非空,是不是一个合适的类型。
如果不可能让这个VIew去显示一个恰当的数据,这个方法会创建一个新的View。
如果我们要做的是一个ListView,在手机上显示的只有那么几条Item,而整个ListView可能有很长,可能是100条
甚至是上万条,总不能让这么多条Item都驻留在内存中,所以android为你准备了一套机制,就是Recycler(反复循
环器),他的具体工作原理可以到 http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html去看。
但是有些地方他没有讲清,所以我再讲一下。先把代码贴出来
布局文件main.xml
此处注意ListView的android:layout_height属性值为"fill_paternt",如果为“wrap_content"将会是另一种情况
adapter的代码ListViewAdapter.java
运行程序之后发现屏幕上显示出的Item的convertview都为空,向下滑动新产生的Item的convetview都不为空。到此为止和上面链接中讲的是一致的,但是如果设置ListView的android:layout_height属性值为“wrap_content
之后,发现只有第一个Item的convertview为null其他的不为空。
虽然两种设置不同,结果也不同,但是convertview的机制没有变。
其实到此为止我们可以总结出convertview的机制了,就是在初始显示的时候,每次显示一个item都调用一次getview方法但是每次调用的时候covertview为空(因为还没有旧的view),当显示完了之后。如果屏幕移动了之后,并且导致有些Item(也可以说是view)跑到屏幕外面,此时如果还有新的item需要产生,则这些item显示时调用的getview方法中的convertview参数就不是null,而是那些移出屏幕的view(旧view),我们所要做的就是将需要显示的item填充到这些回收的view(旧view)中去,最后注意convertview为null的不仅仅是初始显示的那些item,还有一些是已经开始移入屏幕但是还没有view被回收的那些item。
最终我们用亲手写的代码实现了Recycler(反复循环器).
第二个问题其实应该在第一个问题中嵌套来讲,但是为了思路清晰我分开了:
view的setTag和getTag方法其实很简单,在实际编写代码的时候一个view不仅仅是为了显示一些字符串、图片,有时我们还需要他们携带一些其他的数据以便我们对该view的识别或者其他操作。于是android 的设计者们就创造了setTag(Object)方法来存放一些数据和view绑定,我们可以理解为这个是view 的标签也可以理解为view 作为一个容器存放了一些数据。而这些数据我们也可以通过getTag() 方法来取出来。
到这里setTag和getTag大家应该已经明白了。再回到上面的话题,我们通过convertview的setTag方法和getTag方法来将我们要显示的数据来绑定在convertview上。如果convertview 是第一次展示我们就创建新的Holder对象与之绑定,并在最后通过return convertview 返回,去显示;如果convertview 是回收来的那么我们就不必创建新的holder对象,只需要把原来的绑定的holder取出加上新的数据就行了。
附:文中提到的未介绍透彻的另一个帖子:
http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html
工作原理:
如果我们有上亿个项目要显示怎么办?为每个项目创建一个新视图?NO!这不可能!
实际上Android为你缓存了视图。
Android中有个叫做Recycler的构件,下图是他的工作原理:
请看下面的示例代码,这里在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 null 02 - 05 13 : 47 : 32.570 : INFO/System.out( 947 ): getView 1 null 02 - 05 13 : 47 : 32.589 : INFO/System.out( 947 ): getView 2 null 02 - 05 13 : 47 : 32.599 : INFO/System.out( 947 ): getView 3 null 02 - 05 13 : 47 : 32.619 : INFO/System.out( 947 ): getView 4 null 02 - 05 13 : 47 : 32.629 : INFO/System.out( 947 ): getView 5 null 02 - 05 13 : 47 : 32.708 : INFO/System.out( 947 ): getView 6 null 02 - 05 13 : 47 : 32.719 : INFO/System.out( 947 ): getView 7 null 02 - 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 @437447d0 02 - 05 14 : 01 : 31.142 : INFO/System.out( 947 ): getView 12 android.widget.LinearLayout @43744ff8 02 - 05 14 : 01 : 31.279 : INFO/System.out( 947 ): getView 13 android.widget.LinearLayout @43743fa8 02 - 05 14 : 01 : 31.350 : INFO/System.out( 947 ): getView 14 android.widget.LinearLayout @43745820 02 - 05 14 : 01 : 31.429 : INFO/System.out( 947 ): getView 15 android.widget.LinearLayout @43746048 02 - 05 14 : 01 : 31.550 : INFO/System.out( 947 ): getView 16 android.widget.LinearLayout @43746870 02 - 05 14 : 01 : 31.669 : INFO/System.out( 947 ): getView 17 android.widget.LinearLayout @43747098 02 - 05 14 : 01 : 31.839 : INFO/System.out( 947 ): getView 18 android.widget.LinearLayout @437478c0 02 - 05 14 : 03 : 30.900 : INFO/System.out( 947 ): getView 19 android.widget.LinearLayout @43748df0 02 - 05 14 : 03 : 32.069 : INFO/System.out( 947 ): getView 20 android.widget.LinearLayout @437430f8 |
convertView 如我们所期待的非空了,在item11离开屏幕之后,它的视图(@437430f8)作为convertView容纳item21了
我们再举一个稍微复杂的例子,在上例的list中加入一些分隔线
你需要做这些:
以下是代码:
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 = 0 02 - 05 15 : 19 : 03.112 : INFO/System.out( 1035 ): getView 1 null type = 0 02 - 05 15 : 19 : 03.130 : INFO/System.out( 1035 ): getView 2 null type = 0 02 - 05 15 : 19 : 03.141 : INFO/System.out( 1035 ): getView 3 null type = 0 02 - 05 15 : 19 : 03.160 : INFO/System.out( 1035 ): getView 4 null type = 1 02 - 05 15 : 19 : 03.170 : INFO/System.out( 1035 ): getView 5 null type = 0 02 - 05 15 : 19 : 03.180 : INFO/System.out( 1035 ): getView 6 null type = 0 02 - 05 15 : 19 : 03.190 : INFO/System.out( 1035 ): getView 7 null type = 0 02 - 05 15 : 19 : 03.210 : INFO/System.out( 1035 ): getView 8 null type = 0 02 - 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 = 0 02 - 05 15 : 19 : 57.440 : INFO/System.out( 1035 ): getView 11 android.widget.LinearLayout @43744528 type = 0 02 - 05 15 : 20 : 01.310 : INFO/System.out( 1035 ): getView 12 android.widget.LinearLayout @43744eb0 type = 0 02 - 05 15 : 20 : 01.880 : INFO/System.out( 1035 ): getView 13 android.widget.LinearLayout @437456d8 type = 0 02 - 05 15 : 20 : 02.869 : INFO/System.out( 1035 ): getView 14 null type = 1 02 - 05 15 : 20 : 06.489 : INFO/System.out( 1035 ): getView 15 android.widget.LinearLayout @43745f00 type = 0 02 - 05 15 : 20 : 07.749 : INFO/System.out( 1035 ): getView 16 android.widget.LinearLayout @43747170 type = 0 02 - 05 15 : 20 : 10.250 : INFO/System.out( 1035 ): getView 17 android.widget.LinearLayout @43747998 type = 0 02 - 05 15 : 20 : 11.661 : INFO/System.out( 1035 ): getView 18 android.widget.LinearLayout @437481c0 type = 0 02 - 05 15 : 20 : 13.180 : INFO/System.out( 1035 ): getView 19 android.widget.LinearLayout @437468a0 type = 1 02 - 05 15 : 20 : 16.900 : INFO/System.out( 1035 ): getView 20 android.widget.LinearLayout @437489e8 type = 0 02 - 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
附2:上文的英文原文
http://android.amberfog.com/?p=296
How it works:
Next question – what if we have one billion items? Create new view for each item? The answer is NO:-) Android caches views for you.
There’s a component in Android called “Recycler”. I drawed a picture based on Romain Guy presentation at Google IO ’09.
Let’s write a simple code and put System.out to the getView:
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; } } |
Run the program and see what happens:
getView was called 9 times. convertView is null for all visible items
02-05 13:47:32.559: INFO/System.out(947): getView 0 null 02-05 13:47:32.570: INFO/System.out(947): getView 1 null 02-05 13:47:32.589: INFO/System.out(947): getView 2 null 02-05 13:47:32.599: INFO/System.out(947): getView 3 null 02-05 13:47:32.619: INFO/System.out(947): getView 4 null 02-05 13:47:32.629: INFO/System.out(947): getView 5 null 02-05 13:47:32.708: INFO/System.out(947): getView 6 null 02-05 13:47:32.719: INFO/System.out(947): getView 7 null 02-05 13:47:32.729: INFO/System.out(947): getView 8 null |
Then scroll the list slightly down (until item 10 appears):
convertView is still null because there is still no view in the recycler (border of item1 is still visible at the top:))
02-05 13:48:25.169: INFO/System.out(947): getView 9 null |
Let’s scroll list a little more:
Bingo! convertView is not null: item1 goes off the screen directly to the Recycler and item11 is created based on item1.
02-05 13:48:42.879: INFO/System.out(947): getView 10 android.widget.LinearLayout@437430f8 |
scroll more just to check what hapens:
02-05 14:01:31.069: INFO/System.out(947): getView 11 android.widget.LinearLayout@437447d0 02-05 14:01:31.142: INFO/System.out(947): getView 12 android.widget.LinearLayout@43744ff8 02-05 14:01:31.279: INFO/System.out(947): getView 13 android.widget.LinearLayout@43743fa8 02-05 14:01:31.350: INFO/System.out(947): getView 14 android.widget.LinearLayout@43745820 02-05 14:01:31.429: INFO/System.out(947): getView 15 android.widget.LinearLayout@43746048 02-05 14:01:31.550: INFO/System.out(947): getView 16 android.widget.LinearLayout@43746870 02-05 14:01:31.669: INFO/System.out(947): getView 17 android.widget.LinearLayout@43747098 02-05 14:01:31.839: INFO/System.out(947): getView 18 android.widget.LinearLayout@437478c0 02-05 14:03:30.900: INFO/System.out(947): getView 19 android.widget.LinearLayout@43748df0 02-05 14:03:32.069: INFO/System.out(947): getView 20 android.widget.LinearLayout@437430f8 |
convertView is not null as we expected. After item11 goes off the screen, it view (@437430f8) comes as convertView for item 21. simple.
Let’s move to the “more complicated” example. How about to add separator somewhere to the list.
You should do the following:
Simple, isn’t it? Code snippet:
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; } } |
Let’s run what we wrote. Yo will see separators after each 4-th item in the list.
In the log – nothing exceptional – all convertView is null for visible items both types.
02-05 15:19:03.080: INFO/System.out(1035): getView 0 null type = 0 02-05 15:19:03.112: INFO/System.out(1035): getView 1 null type = 0 02-05 15:19:03.130: INFO/System.out(1035): getView 2 null type = 0 02-05 15:19:03.141: INFO/System.out(1035): getView 3 null type = 0 02-05 15:19:03.160: INFO/System.out(1035): getView 4 null type = 1 02-05 15:19:03.170: INFO/System.out(1035): getView 5 null type = 0 02-05 15:19:03.180: INFO/System.out(1035): getView 6 null type = 0 02-05 15:19:03.190: INFO/System.out(1035): getView 7 null type = 0 02-05 15:19:03.210: INFO/System.out(1035): getView 8 null type = 0 02-05 15:19:03.210: INFO/System.out(1035): getView 9 null type = 1 |
Scroll list and see what happens:
02-05 15:19:54.160: INFO/System.out(1035): getView 10 null type = 0 02-05 15:19:57.440: INFO/System.out(1035): getView 11 android.widget.LinearLayout@43744528 type = 0 02-05 15:20:01.310: INFO/System.out(1035): getView 12 android.widget.LinearLayout@43744eb0 type = 0 02-05 15:20:01.880: INFO/System.out(1035): getView 13 android.widget.LinearLayout@437456d8 type = 0 02-05 15:20:02.869: INFO/System.out(1035): getView 14 null type = 1 02-05 15:20:06.489: INFO/System.out(1035): getView 15 android.widget.LinearLayout@43745f00 type = 0 02-05 15:20:07.749: INFO/System.out(1035): getView 16 android.widget.LinearLayout@43747170 type = 0 02-05 15:20:10.250: INFO/System.out(1035): getView 17 android.widget.LinearLayout@43747998 type = 0 02-05 15:20:11.661: INFO/System.out(1035): getView 18 android.widget.LinearLayout@437481c0 type = 0 02-05 15:20:13.180: INFO/System.out(1035): getView 19 android.widget.LinearLayout@437468a0 type = 1 02-05 15:20:16.900: INFO/System.out(1035): getView 20 android.widget.LinearLayout@437489e8 type = 0 02-05 15:20:25.690: INFO/System.out(1035): getView 21 android.widget.LinearLayout@4374a8d8 type = 0 |
convertView is null for separator view type until first separator is visible. When it goes off the screen – view also comes to the Recycler and convertView comes to play.
BaseAdapter中重写getview的心得以及发现convertView回收的机制,布布扣,bubuko.com
BaseAdapter中重写getview的心得以及发现convertView回收的机制
标签:android style blog http color java 使用 os
原文地址:http://www.cnblogs.com/zhzhy126/p/3928072.html