Android如果动态改变CursorAdapter Item个数
//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); } }
外部供重写的方法:
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); }
//子类实现部分:
需要改变的偏移量
private int getPositionOffset() { return mDefaultOffset; }
重写方法:
@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是几,这个数组长度就是几,类型一一对应
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。