ListView中有不同的ViewType,它的convertView实现
来源:互联网 发布:新浪微博登录网络异常 编辑:程序博客网 时间:2024/04/29 23:24
a_long_总结:
只要在继承BaseAdapter的MyAdapter中完成以下3点:
1:重写getViewTypeCount()-总共的Type数量;
2:重写getItemViewType(int)-由position来得知Type的id;
3:根据Type在getView中正确的创建convertView;
那么Adapter会调用getTimeViewType方法来判断这个View的类型,从而当View被Recycler回收后,Adapter会自动根据View的Type而正确的返回是否有convertView或者是convertView==null。
这样就实现了一个ListView中有不同的ViewType,而每一个View都被正确的convert(VIewHolder)了,优化性能。
示例代码如下:
参考自:
英文原文:http://android.amberfog.com/?p=296
Different list items’ layouts
Let’s move to the “more complicated” example. How about to add separator somewhere to the list.
You should do the following:
- Override getViewTypeCount() – return how many different view layouts you have
- Override getItemViewType(int) – return correct view type id by position
- Create correct convertView (depending on view item type) in getView
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 = 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
Scroll list and see what happens:
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 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.
0 0