标签:
Adapter的作用是界面与数据之间的桥梁,通过设置适配器至ListView控件后(如调用ListView的 setAdapter(ListAdapter adapter)
),列表的每一项会显示至页面中。其实,当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View,如:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
CursorAdapter中提供了这两个抽象方法:
//Makes a new view to hold the data pointed to by cursor.
public abstract View newView(Context context, Cursor cursor, ViewGroup parent);
//Bind an existing view to the data pointed to by cursor
public abstract void bindView(View view, Context context, Cursor cursor);
newView和bindView细化了getView中的功能实现,均可写在getView中代替。
三者的调用顺序为:
getView——>newView——>bindView
转:http://hi.baidu.com/spare_h/blog/item/a826d9d873f7053410df9b52.html
ListView 中getView的原理
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进行输出
01 |
publicclassMultipleItemsList extendsListActivity { |
03 |
privateMyCustomAdapter mAdapter; |
06 |
publicvoidonCreate(Bundle savedInstanceState) { |
07 |
super.onCreate(savedInstanceState); |
08 |
mAdapter = newMyCustomAdapter(); |
09 |
for(inti = 0; i < 50; i++) { |
10 |
mAdapter.addItem("item "+ i); |
12 |
setListAdapter(mAdapter); |
15 |
privateclassMyCustomAdapter extendsBaseAdapter { |
17 |
privateArrayList mData = newArrayList(); |
18 |
privateLayoutInflater mInflater; |
20 |
publicMyCustomAdapter() { |
21 |
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
24 |
publicvoidaddItem(finalString item) { |
26 |
notifyDataSetChanged(); |
35 |
publicString getItem(intposition) { |
36 |
returnmData.get(position); |
40 |
publiclonggetItemId(intposition) { |
45 |
publicView getView(intposition, View convertView, ViewGroup parent) { |
46 |
System.out.println("getView "+ position + " "+ convertView); |
47 |
ViewHolder holder = null; |
48 |
if(convertView == null) { |
49 |
convertView = mInflater.inflate(R.layout.item1, null); |
50 |
holder = newViewHolder(); |
51 |
holder.textView = (TextView)convertView.findViewById(R.id.text); |
52 |
convertView.setTag(holder); |
54 |
holder = (ViewHolder)convertView.getTag(); |
56 |
holder.textView.setText(mData.get(position)); |
62 |
publicstaticclassViewHolder { |
63 |
publicTextView textView; |
执行程序,然后在Logcat中查看日志
getView 被调用 9 次 ,convertView 对于所有的可见项目是空值(如下)
02-0513:47:32.559: INFO/System.out(947): getView 0null |
02-0513:47:32.570: INFO/System.out(947): getView 1null |
02-0513:47:32.589: INFO/System.out(947): getView 2null |
02-0513:47:32.599: INFO/System.out(947): getView 3null |
02-0513:47:32.619: INFO/System.out(947): getView 4null |
02-0513:47:32.629: INFO/System.out(947): getView 5null |
02-0513:47:32.708: INFO/System.out(947): getView 6null |
02-0513:47:32.719: INFO/System.out(947): getView 7null |
02-0513:47:32.729: INFO/System.out(947): getView 8null |
然后稍微向下滚动List,直到item10出现:
convertView仍然是空值,因为recycler中没有视图(item1的边缘仍然可见,在顶端)
02-0513:48:25.169: INFO/System.out(947): getView 9null |
再滚动List
convertView不是空值了!item1离开屏幕到Recycler中去了,然后item11被创建
02-0513:48:42.879: INFO/System.out(947): getView 10android.widget.LinearLayout@437430f8 |
再滚动:
02-0514:01:31.069: INFO/System.out(947): getView 11android.widget.LinearLayout@437447d0 |
02-0514:01:31.142: INFO/System.out(947): getView 12android.widget.LinearLayout@43744ff8 |
02-0514:01:31.279: INFO/System.out(947): getView 13android.widget.LinearLayout@43743fa8 |
02-0514:01:31.350: INFO/System.out(947): getView 14android.widget.LinearLayout@43745820 |
02-0514:01:31.429: INFO/System.out(947): getView 15android.widget.LinearLayout@43746048 |
02-0514:01:31.550: INFO/System.out(947): getView 16android.widget.LinearLayout@43746870 |
02-0514:01:31.669: INFO/System.out(947): getView 17android.widget.LinearLayout@43747098 |
02-0514:01:31.839: INFO/System.out(947): getView 18android.widget.LinearLayout@437478c0 |
02-0514:03:30.900: INFO/System.out(947): getView 19android.widget.LinearLayout@43748df0 |
02-0514:03:32.069: INFO/System.out(947): getView 20android.widget.LinearLayout@437430f8 |
convertView 如我们所期待的非空了,在item11离开屏幕之后,它的视图(@437430f8)作为convertView容纳item21了
ListView优化
http://blog.csdn.net/yucf1988/article/details/6365297
CursorAdapter中getView newView bindView异同
标签:
原文地址:http://www.cnblogs.com/bdbw2012/p/4633035.html