首页 > 移动平台 > 详细

Android如果动态改变CursorAdapter Item个数

时间:2014-02-10 23:49:16      阅读:786      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
//adapter内部类
private class SearchAdapter extends CursorAdapter { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return newAdapterItemView(context, cursor, parent); } @Override public void bindView(View view, Context context, Cursor cursor) { bindAdapterItemView(view, context, cursor); } public int getSuperCount(){ return super.getCount(); } public View getSuperView(int position, View convertView, ViewGroup parent){ return super.getView(position, convertView, parent); } public int getSuperItemViewType(int position) { return super.getItemViewType(position); } public int getSuperViewTypeCount() { return super.getViewTypeCount(); } public long getSuperItemId(int position) { return super.getItemId(position); } @Override public int getCount() { return adapterGetCount(); } @Override public View getView(int position, View convertView, ViewGroup parent) { return adapterGetView(position, convertView, parent); } @Override public int getItemViewType(int position) { return adapterGetItemViewType(position); } @Override public int getViewTypeCount() { return adapterGetViewTypeCount(); } @Override public long getItemId(int position) { return adapterGetItemId(position); } }
bubuko.com,布布扣

 

外部供重写的方法:

bubuko.com,布布扣
    protected int adapterGetCount(){
        return mAdapter.getSuperCount();
    }
    protected View adapterGetView(int position, View convertView, ViewGroup parent){
        return mAdapter.getSuperView(position, convertView, parent);
    }
    protected int adapterGetItemViewType(int position) {
        return mAdapter.getSuperItemViewType(position);
    }
    protected int adapterGetViewTypeCount() {
        return mAdapter.getSuperViewTypeCount();
    }
    protected long adapterGetItemId(int position) {
        return mAdapter.getSuperItemId(position);
    }
bubuko.com,布布扣

 

//子类实现部分:

 

需要改变的偏移量

 private int getPositionOffset() {
return mDefaultOffset;        
 }

 

重写方法:

bubuko.com,布布扣
@Override
    protected int adapterGetCount() {

        return super.adapterGetCount() + getPositionOffset();
    }

    @Override
    protected View adapterGetView(int position, View convertView, ViewGroup parent) {

        if (position < getPositionOffset()) {
            int type = mDefaultType[position];
            switch (type) {
                case ITEM_TYPE_1:
                    return getView1();
                case ITEM_TYPE_2:
                    return getView2();
                case ITEM_TYPE_3:
                    return getView3();
                case ITEM_TYPE_4:
                    return getView4();
                default:
                    break;
            }
        }
        return super.adapterGetView(position - getBookmarkPositionOffset(), convertView, parent);
    }
@Override
    protected int adapterGetItemViewType(int position) {

        if (position < getPositionOffset()) {
            return mDefaultType[position];
        }
        return getItemViewType(position - getPositionOffset());
    }

    @Override
    protected int adapterGetViewTypeCount() {

        return ITEM_TYPE_COUNT + getPositionOffset();
    }

    @Override
    public long adapterGetItemId(int position) {

        if (position < getPositionOffset()) {
            int type = mDefaultType[position];
            switch (type) {
                case ITEM_TYPE_1:
                    return -1;
                case ITEM_TYPE_2:
                    return  -2;
                case ITEM_TYPE_3:
                    return  -3;
                case ITEM_TYPE_4:
                    return -4;
                default:
                    break;
            }
        }
        return super.adapterGetItemId(position - getPositionOffset());
    }

  protected static final int ITEM_TYPE_1 = -101;
    protected static final int ITEM_TYPE_2 = -102;
    protected static final int ITEM_TYPE_3 = -103;
    protected static final int ITEM_TYPE_4 = -104;

    private static int mDefaultOffset;
    private static int[] mDefaultType = null;//这个需要初始化,mDefaultOffset是几,这个数组长度就是几,类型一一对应

bubuko.com,布布扣

Android如果动态改变CursorAdapter Item个数

原文:http://www.cnblogs.com/lchd/p/3543404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!