1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| package com.github.jason.demos; import android.util.Pair; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.ArrayList; import java.util.List;
public abstract class GroupAdapter<T, S> extends BaseAdapter { private List<Pair<T, List<S>>> mList = new ArrayList<>(); private List<Integer> mGroupPositions = new ArrayList<>(); public GroupAdapter(List<Pair<T, List<S>>> list) { mList = list ; calcGroupPositions(); } protected final void setList(List<Pair<T, List<S>>> list, boolean noitfy){ if(mList != null){ mList = null; } mList = list; calcGroupPositions(); if(noitfy){ notifyDataSetChanged(); } } abstract public View getChildView(int listPosition, int childPosition, int groupPosition , View convertView, ViewGroup parent); abstract public View getGroupView(int listPosition, int groupPosition, View convertView, ViewGroup parent); @Override public int getItemViewType(int position) { return isGroup(position) ? 1 : 0; } @Override public int getViewTypeCount() { return 2; } @Override public int getCount() { return calcCount(); } @Override public final S getItem(int position) { if (isGroup(position)) { return null; } int group = getGroupPosition(position); int child = getChildPosition(position); return getChild(group,child); } @Override public final long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (isGroup(position)) { return getGroupView(position, getGroupPosition(position), convertView, parent); } else { return getChildView(position, getChildPosition(position), getGroupPosition(position), convertView, parent); } } public final S getChild(int groupPosition, int childPosition) { if (groupPosition < 0 || groupPosition>=mList.size()) { return null; } if (childPosition < 0 || childPosition >= mList.get(groupPosition).second.size()) { return null; } return mList.get(groupPosition).second.get(childPosition); } public final T getGroup(int groupPosition) { if (groupPosition < 0 || groupPosition>=mList.size()) { return null; } return mList.get(groupPosition).first; } public final boolean isGroup(int position) { int N = mGroupPositions.size(); for (int i=0 ; i<N ; i++) { if (position == mGroupPositions.get(i)) return true; } return false; } public final int getGroupPosition(int position) { int N = mGroupPositions.size(); for (int i=0 ; i<N ; i++) { if (position < mGroupPositions.get(i)) return i-1; } return N-1; } public final int getPositionByGroupPosition(int groupPosition) { if (groupPosition >= mList.size()) { return 0; } return mGroupPositions.get(groupPosition); } public final int getChildPosition(int position) { int group = -1; int N = mGroupPositions.size(); for (int i=0 ; i<N ; i++) { if (position < mGroupPositions.get(i)) { group = mGroupPositions.get(i-1); break; } } if (group == -1) { group = mGroupPositions.get(N-1); } return position - group - 1; } private void calcGroupPositions() { int len = mList.size(); int total = 0; mGroupPositions.clear(); for (int i = 0 ; i<len ; i++) { Pair<T, List<S>> pair = mList.get(i); mGroupPositions.add(total); total += pair.second.size()+1; } } private int calcCount() { return mGroupPositions.get(mGroupPositions.size() - 1 ) + mList.get(mList.size()-1).second.size(); } }
|