/
BaseAdapter.java
197 lines (176 loc) · 4.62 KB
/
BaseAdapter.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package net.devwiki.recycler;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
/**
* 基础的Adapter
* <p>
* Created by DevWiki on 2016/7/13.
*/
public abstract class BaseAdapter<M, VH extends BaseHolder> extends AbsAdapter<M, VH> {
private List<M> dataList;
public BaseAdapter(Context context) {
super(context);
this.dataList = new ArrayList<>();
}
public BaseAdapter(Context context, List<M> list) {
super(context);
this.dataList = new ArrayList<>();
this.dataList.addAll(list);
}
/**
* 填充数据,此操作会清除原来的数据
*
* @param list 要填充的数据
* @return true:填充成功并调用刷新数据
*/
public boolean fillList(List<M> list) {
dataList.clear();
boolean result = dataList.addAll(list);
if (result) {
notifyDataSetChanged();
}
return result;
}
/**
* 追加一条数据
*
* @param data 要追加的数据
* @return true:追加成功并刷新界面
*/
public boolean appendItem(M data) {
boolean result = dataList.add(data);
if (result) {
if (getHeaderExtraViewCount() == 0) {
notifyItemInserted(dataList.size() - 1);
} else {
notifyItemInserted(dataList.size());
}
}
return result;
}
/**
* 追加集合数据
*
* @param list 要追加的集合数据
* @return 追加成功并刷新
*/
public boolean appendList(List<M> list) {
boolean result = dataList.addAll(list);
if (result) {
notifyDataSetChanged();
}
return result;
}
/**
* 在最顶部前置数据
*
* @param data 要前置的数据
*/
public void proposeItem(M data) {
dataList.add(0, data);
if (getHeaderExtraViewCount() == 0) {
notifyItemInserted(0);
} else {
notifyItemInserted(getHeaderExtraViewCount());
}
}
/**
* 在顶部前置数据集合
*
* @param list 要前置的数据集合
*/
public void proposeList(List<M> list) {
dataList.addAll(0, list);
notifyDataSetChanged();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public final int getItemViewType(int position) {
if (headerView != null && position == 0) {
return VIEW_TYPE_HEADER;
} else if (footerView != null && position == dataList.size() + getHeaderExtraViewCount()) {
return VIEW_TYPE_FOOTER;
} else {
return getCustomViewType(position);
}
}
/**
* 获取自定义View的类型
*
* @param position 位置
* @return View的类型
*/
public abstract int getCustomViewType(int position);
@Override
public int getItemCount() {
return dataList.size() + getExtraViewCount();
}
/**
* 根据位置获取一条数据
*
* @param position View的位置
* @return 数据
*/
public M getItem(int position) {
if (headerView != null && position == 0
|| position >= dataList.size() + getHeaderExtraViewCount()) {
return null;
}
return headerView == null ? dataList.get(position) : dataList.get(position - 1);
}
/**
* 根据ViewHolder获取数据
*
* @param holder ViewHolder
* @return 数据
*/
public M getItem(VH holder) {
return getItem(holder.getAdapterPosition());
}
public void updateItem(M data) {
int index = dataList.indexOf(data);
if (index < 0) {
return;
}
dataList.set(index, data);
if (headerView == null) {
notifyItemChanged(index);
} else {
notifyItemChanged(index + 1);
}
}
/**
* 移除一条数据
*
* @param position 位置
*/
public void removeItem(int position) {
if (headerView == null) {
dataList.remove(position);
} else {
dataList.remove(position - 1);
}
notifyItemRemoved(position);
}
/**
* 移除一条数据
*
* @param data 要移除的数据
*/
public void removeItem(M data) {
int index = dataList.indexOf(data);
if (index < 0) {
return;
}
dataList.remove(index);
if (headerView == null) {
notifyItemRemoved(index);
} else {
notifyItemRemoved(index + 1);
}
}
}