/
ExpandableListViewDividerAdapter.java
171 lines (139 loc) · 5.09 KB
/
ExpandableListViewDividerAdapter.java
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.nalan.swipeitem.listview;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import com.nalan.swipeitem.R;
/**
* Author: liyi
* Date: 2017/2/26.
*/
public class ExpandableListViewDividerAdapter extends BaseExpandableListAdapter {
private BaseExpandableListAdapter mMainAdapter;
private int mGroupDivider;
private int mChildDivider;
public ExpandableListViewDividerAdapter(BaseExpandableListAdapter mainAdapter){
this(mainAdapter, R.layout.expandablelistview_group_divider,R.layout.expandablelistview_child_divider);
}
public ExpandableListViewDividerAdapter(BaseExpandableListAdapter mainAdapter, int groupDivider, int childDivider){
mMainAdapter = mainAdapter;
mGroupDivider = groupDivider;
mChildDivider = childDivider;
}
@Override
public boolean areAllItemsEnabled() {
return mMainAdapter.areAllItemsEnabled();
}
@Override
public boolean isEmpty() {
return mMainAdapter.isEmpty();
}
@Override
public void notifyDataSetChanged() {
mMainAdapter.notifyDataSetChanged();
}
@Override
public void notifyDataSetInvalidated() {
mMainAdapter.notifyDataSetInvalidated();
}
@Override
public void onGroupCollapsed(int groupPosition) {
mMainAdapter.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
mMainAdapter.onGroupExpanded(groupPosition);
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
mMainAdapter.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
mMainAdapter.unregisterDataSetObserver(observer);
}
@Override
public int getGroupCount() {
int groupCount = mMainAdapter.getGroupCount();
if(groupCount<2)
return groupCount;
else
return groupCount*2-1;
}
@Override
public int getChildrenCount(int groupPosition) {
if(isEven(groupPosition)){
int childCount = mMainAdapter.getChildrenCount(groupPosition/2);
return childCount*2;
}else
return 0;
}
@Override
public Object getGroup(int groupPosition) {
if(isEven(groupPosition))
return mMainAdapter.getGroup(groupPosition/2);
else
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
if(isEven(groupPosition) && !isEven(childPosition))//child奇数位置才是因为它的头尾都会添加分隔线
return mMainAdapter.getChild(groupPosition/2,childPosition/2);
else
return "";
}
@Override
public long getGroupId(int groupPosition) {
return getCombinedGroupId(groupPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return getCombinedChildId(groupPosition,childPosition);
}
@Override
public boolean hasStableIds() {
return mMainAdapter.hasStableIds();
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return isEven(groupPosition) && !isEven(childPosition) ? mMainAdapter.getChildType(groupPosition/2,childPosition/2):getChildTypeCount()-1;
}
@Override
public int getChildTypeCount() {
return mMainAdapter.getChildTypeCount()+1;
}
@Override
public int getGroupTypeCount() {
return mMainAdapter.getGroupTypeCount()+1;
}
@Override
public int getGroupType(int groupPosition) {
return isEven(groupPosition) ? mMainAdapter.getGroupType(groupPosition/2):getGroupTypeCount()-1;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if(isEven(groupPosition))
return mMainAdapter.getGroupView(groupPosition/2,isExpanded,convertView,parent);
if(convertView==null && getGroupType(groupPosition)==getGroupTypeCount()-1)
return LayoutInflater.from(parent.getContext()).inflate(mGroupDivider,parent,false);
else
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if(isEven(groupPosition) && !isEven(childPosition))
return mMainAdapter.getChildView(groupPosition/2,childPosition/2,isLastChild,convertView,parent);
if(convertView==null && getChildType(groupPosition,childPosition)==getChildTypeCount()-1)
return LayoutInflater.from(parent.getContext()).inflate(mChildDivider,parent,false);
else
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return mMainAdapter.isChildSelectable(groupPosition/2,childPosition/2);
}
private boolean isEven(int index){
return index%2==0;
}
}