This repository was archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathSimpleAdapter.java
More file actions
313 lines (256 loc) · 8.17 KB
/
SimpleAdapter.java
File metadata and controls
313 lines (256 loc) · 8.17 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.jaychang.srv;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.jaychang.srv.behavior.DragAndDropHelper;
import com.jaychang.srv.behavior.OnItemDismissListener;
import com.jaychang.srv.behavior.OnItemMoveListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@SuppressWarnings("unchecked")
public class SimpleAdapter extends RecyclerView.Adapter<SimpleViewHolder>
implements OnItemMoveListener, OnItemDismissListener, CellOperations {
private List<SimpleCell> cells;
private SparseArray<SimpleCell> cellTypeMap;
private DragAndDropHelper dragAndDropHelper;
SimpleAdapter() {
this.cells = new ArrayList<>();
this.cellTypeMap = new SparseArray<>();
setHasStableIds(true);
}
void setDragAndDropHelper(DragAndDropHelper dragAndDropHelper) {
this.dragAndDropHelper = dragAndDropHelper;
}
@Override
public SimpleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
SimpleCell cell = cellTypeMap.get(viewType);
View view = LayoutInflater.from(parent.getContext()).inflate(cell.getLayoutRes(), parent, false);
final SimpleViewHolder viewHolder = cell.onCreateViewHolder(parent, view);
if (dragAndDropHelper != null && dragAndDropHelper.getDragHandleId() != 0) {
View dragHandle = viewHolder.itemView.findViewById(dragAndDropHelper.getDragHandleId());
dragHandle.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
dragAndDropHelper.onStartDrag(viewHolder);
}
return false;
}
});
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final SimpleViewHolder holder, @NonNull int position, @Nullable List<Object> payloads) {
final SimpleCell cell = cells.get(position);
holder.bind(cell);
if (cell.getOnCellClickListener() != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cell.getOnCellClickListener().onCellClicked(cell.getItem());
}
});
}
if (cell.getOnCellLongClickListener() != null) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
cell.getOnCellLongClickListener().onCellLongClicked(cell.getItem());
return true;
}
});
}
Object payload = null;
if (payloads != null && payloads.size() > 0) {
payload = payloads.get(0);
}
cell.onBindViewHolder(holder, position, holder.itemView.getContext(), payload);
}
@Override
public void onBindViewHolder(@NonNull SimpleViewHolder holder, int position) {
onBindViewHolder(holder, position, null);
}
@Override
public void onViewRecycled(@NonNull SimpleViewHolder holder) {
holder.getCell().onUnbindViewHolder(holder);
holder.unbind();
}
@Override
public int getItemCount() {
return cells.size();
}
@Override
public int getItemViewType(int position) {
return getItemViewType(cells.get(position));
}
private int getItemViewType(SimpleCell cell) {
return cell.getItem().getClass().getName().hashCode();
}
@Override
public long getItemId(int position) {
return cells.get(position).getItemId();
}
private void addCellType(SimpleCell cell) {
if (!isCellTypeAdded(cell)) {
cellTypeMap.put(getItemViewType(cell), cell);
}
}
private void addCellTypes(List<SimpleCell> cells) {
for (SimpleCell cell : cells) {
addCellType(cell);
}
}
private void removeCellType(SimpleCell cell) {
boolean hasCellType = false;
for (SimpleCell simpleCell : cells) {
if (simpleCell.getClass().equals(cell.getClass())) {
hasCellType = true;
}
}
if (isCellTypeAdded(cell) && !hasCellType) {
cellTypeMap.remove(getItemViewType(cell));
}
}
private boolean isCellTypeAdded(SimpleCell cell) {
return cellTypeMap.indexOfKey(getItemViewType(cell)) >= 0;
}
@Override
public void addCell(SimpleCell cell) {
addCell(cells.size(), cell);
}
@Override
public void addCell(int atPosition, SimpleCell cell) {
cells.add(atPosition, cell);
addCellType(cell);
notifyItemInserted(atPosition);
}
@Override
public void addCells(List<? extends SimpleCell> cells) {
if (cells.isEmpty()) {
notifyDataSetChanged();
return;
}
int initialSize = this.cells.size();
for (SimpleCell cell : cells) {
this.cells.add(cell);
addCellType(cell);
}
notifyItemRangeInserted(initialSize, cells.size());
}
@Override
public void addCells(SimpleCell... cells) {
addCells(Arrays.asList(cells));
}
@Override
public void addCells(int fromPosition, List<? extends SimpleCell> cells) {
if (cells.isEmpty()) {
notifyDataSetChanged();
return;
}
int pos = fromPosition;
for (SimpleCell cell : cells) {
this.cells.add(pos++, cell);
addCellType(cell);
}
notifyItemRangeInserted(fromPosition, cells.size());
}
@Override
public void addCells(int fromPosition, SimpleCell... cells) {
addCells(fromPosition, Arrays.asList(cells));
}
@Override
public <T extends SimpleCell & Updatable> void addOrUpdateCell(T cell) {
addOrUpdateCells(Collections.singletonList(cell));
}
@Override
public <T extends SimpleCell & Updatable> void addOrUpdateCells(List<T> cells) {
SimpleDiffCallbackDelegate callbackDelegate = new SimpleDiffCallbackDelegate(this, cells);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callbackDelegate);
diffResult.dispatchUpdatesTo(this);
}
@Override
public <T extends SimpleCell & Updatable> void addOrUpdateCells(T... cells) {
addOrUpdateCells(Arrays.asList(cells));
}
@Override
public void removeCell(SimpleCell cell) {
int position = cells.indexOf(cell);
cells.remove(cell);
removeCellType(cell);
notifyItemRemoved(position);
}
@Override
public void removeCell(int atPosition) {
removeCell(cells.get(atPosition));
}
@Override
public void removeCells(int fromPosition, int toPosition) {
for (int i = toPosition; i >= fromPosition; i--) {
SimpleCell cell = cells.get(i);
cells.remove(cell);
removeCellType(cell);
}
notifyItemRangeRemoved(fromPosition, toPosition - fromPosition + 1);
}
@Override
public void removeCells(int fromPosition) {
removeCells(fromPosition, cells.size() - 1);
}
@Override
public void removeAllCells() {
cells.clear();
cellTypeMap.clear();
notifyDataSetChanged();
}
@Override
public void updateCell(int atPosition, Object payloads) {
notifyItemChanged(atPosition, payloads);
}
@Override
public void updateCells(int fromPosition, int toPosition, Object payloads) {
notifyItemRangeChanged(fromPosition, toPosition - fromPosition + 1, payloads);
}
@Override
public SimpleCell getCell(int atPosition) {
return cells.get(atPosition);
}
@Override
public List<SimpleCell> getCells(int fromPosition, int toPosition) {
return cells.subList(fromPosition, toPosition + 1);
}
@Override
public List<SimpleCell> getAllCells() {
return cells;
}
public boolean isEmpty() {
return cells.isEmpty();
}
public int getCellCount() {
return cells.size();
}
void setCells(List<? extends SimpleCell> cells) {
this.cells.clear();
this.cells.addAll(cells);
addCellTypes(this.cells);
}
@Override
public void onItemMoved(int fromPosition, int toPosition) {
Collections.swap(cells, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onItemDismissed(int position) {
cells.remove(position);
notifyItemRemoved(position);
}
}