-
Notifications
You must be signed in to change notification settings - Fork 57
/
SimpleAdapter.java
151 lines (125 loc) · 3.56 KB
/
SimpleAdapter.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
package com.anarchy.classify.simple;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.anarchy.classify.R;
import java.util.ArrayList;
import java.util.List;
/**
* <p/>
* Date: 16/6/7 11:55
* Author: rsshinide38@163.com
* <p/>
*/
public abstract class SimpleAdapter<T, VH extends SimpleAdapter.ViewHolder> extends PrimitiveSimpleAdapter<List<T>, VH> {
protected List<List<T>> mData;
public SimpleAdapter(List<List<T>> data) {
mData = data;
}
@SuppressWarnings("unchecked")
@Override
protected VH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_item, parent, false);
return (VH) new ViewHolder(view);
}
/**
* 返回主层级数量
*
* @return
*/
@Override
protected int getItemCount() {
if (mData == null) return 0;
return mData.size();
}
/**
* 副层级的数量,用于主层级上的显示效果
*
* @return
*/
@Override
protected int getSubItemCount(int parentPosition) {
if(mData == null) return 0;
return mData.get(parentPosition).size();
}
@Override
protected List<T> getSubSource(int parentPosition) {
return mData.get(parentPosition);
}
@Override
protected boolean canExplodeItem(int position, View pressedView) {
if (position < mData.size() && mData.get(position).size() > 1) {
return true;
}
return false;
}
/**
* 在主层级触发move事件 在这里进行数据改变
*
* @param selectedPosition 当前选择的item位置
* @param targetPosition 要移动到的位置
*/
@Override
protected void onMove(int selectedPosition, int targetPosition) {
List<T> list = mData.remove(selectedPosition);
mData.add(targetPosition, list);
}
@Override
protected void onSubMove(List<T> ts, int selectedPosition, int targetPosition) {
ts.add(targetPosition, ts.remove(selectedPosition));
}
@Override
protected int onLeaveSubRegion(int parentPosition,List<T> ts, int selectedPosition) {
List<T> list = new ArrayList<>();
list.add(ts.remove(selectedPosition));
mData.add(list);
return mData.size() - 1;
}
/**
* 两个选项能否合并
*
* @param selectPosition
* @param targetPosition
* @return
*/
@Override
protected boolean canMergeItem(int selectPosition, int targetPosition) {
List<T> currentSelected = mData.get(selectPosition);
return currentSelected.size() < 2;
}
/**
* 合并数据处理
*
* @param selectedPosition
* @param targetPosition
*/
@Override
protected void onMerged(int selectedPosition, int targetPosition) {
mData.get(targetPosition).add(mData.get(selectedPosition).get(0));
mData.remove(selectedPosition);
}
/**
* 主层级数据绑定
*
* @param holder
* @param position
*/
@Override
protected void onBindMainViewHolder(VH holder, int position) {
}
/**
* 副层级数据绑定
*
* @param holder
* @param mainPosition
* @param subPosition
*/
@Override
protected void onBindSubViewHolder(VH holder, int mainPosition, int subPosition) {
}
public static class ViewHolder extends PrimitiveSimpleAdapter.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}