一、资源
对了解 Adapter的机制很有帮助,而且此博文中介绍了一种不同类型的item的处理办法
二、实现效果
三、分析重点
GridView很容易实现宫格式的UI,很多用法可以跟ListView类比。要实现Footer的话,ListView有包装好的addFooter()方法,GridView实现Footer貌似没有直接的方法(有的话我就要流泪了...)。
重点:
- 如果直接重写BaseAdapter 里的通过getCount()返回值,去决定调用getView()的最终次数
那么有一种方法getCount()返回值为data.size()+1;再在getView中根据postion和data.size()比较,找到那个Footer的getView调用,再处理
- 但是现在用的是ResourceCursorAdapter,ResourceCursorAdapter监听Cursor、数据库的变化什么的...于是很蛋疼得开始用重写BaseAdapter解决的东西又来搞一遍
BaseAdapter <-CursorAdapter<-ResourceCursorAdapter,众所周知,CursorAdapter用newView(),bindView()来处理
查看CursorAdapter源码,可以发现其内部运行机制
View Code
1 /**
2 * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
3 */
4 public View getView(int position, View convertView, ViewGroup parent) {
5 if (!mDataValid) {
6 throw new IllegalStateException("this should only be called when the cursor is valid");
7 }
8 if (!mCursor.moveToPosition(position)) {
9 throw new IllegalStateException("couldn't move cursor to position " + position);
10 }
11 View v;
12 if (convertView == null) {
13 v = newView(mContext, mCursor, parent);
14 } else {
15 v = convertView;
16 }
17 bindView(v, mContext, mCursor);
18 return v;
19 }
可以发现,bindView填充数据时的cursor其实是在getview中进行定位操作的,而且只有再getView中才能获取pos,所以,办法是,重写getView(),给异类做个标志,然后根据标志为在bindView中进行处理。
View Code
1 /* (non-Javadoc)
2 * @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
3 */
4 @Override
5 public void bindView(View view, Context context, Cursor cursor) {
6 // TODO Auto-generated method stub
7 final ViewHolder cache = (ViewHolder) view.getTag();
8 if(ITEM_MORE==mType){
9 cache.image_icon.setImageBitmap(
10 ImageUtil.ReflectionImage(context.getResources().getDrawable(R.drawable.icon)));
11 cache.tx_feedname.setText("更多内容");
12 }
13 else{
14 cache.image_icon.setImageBitmap(
15 ImageUtil.ReflectionImage(context.getResources().getDrawable(R.drawable.letv)));
16 cache.tx_feedname.setText(cursor.getString(index[2]));
17 }
18
19 }
20
21 /* (non-Javadoc)
22 * @see android.widget.ResourceCursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
23 */
24 @Override
25 public View newView(Context context, Cursor cursor, ViewGroup parent) {
26 // TODO Auto-generated method stub
27 View view = super.newView(context, cursor, parent);
28 ViewHolder holder = new ViewHolder();
29 holder.image_icon=(ImageView) view.findViewById(R.id.iv_Icon);
30 holder.tx_feedname=(TextView) view.findViewById(R.id.tv_Name);
31 view.setTag(holder);
32 return view;
33 }
34
35 private class ViewHolder{
36
37 private ImageView image_icon;
38 private TextView tx_feedname;
39 }
40
41 public int getCount() {
42
43
44 return (mCursor.getCount()+1);
45
46 }
47
48
49 public View getView(int position, View convertView, ViewGroup parent) {
50
51 if(mCursor==null){
52 throw new IllegalStateException("this should only be called when the cursor is valid");
53 }
54
55 if (!mCursor.moveToPosition(position)&&position==(getCount()-1)) {
56 mCursor.move(-1);
57 //标志footer出现
58 mType=ITEM_MORE;
59 }
60 View v;
61 if (convertView == null) {
62 v = newView(mContext, mCursor, parent);
63 } else {
64 v = convertView;
65 }
66 bindView(v, mContext, mCursor);
67 return v;
68 }
运行OK。