-
Notifications
You must be signed in to change notification settings - Fork 57
/
BaseMainAdapter.java
198 lines (168 loc) · 8.17 KB
/
BaseMainAdapter.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
198
package com.anarchy.classify.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.VelocityTracker;
import android.view.View;
import com.anarchy.classify.MergeInfo;
import com.anarchy.classify.ClassifyView;
import com.anarchy.classify.callback.MainRecyclerViewCallBack;
import com.anarchy.classify.callback.SubRecyclerViewCallBack;
import java.util.ArrayList;
import java.util.List;
/**
* <p/>
* Date: 16/6/1 15:33
* Author: rsshinide38@163.com
* <p/>
*/
public abstract class BaseMainAdapter<VH extends RecyclerView.ViewHolder, Sub extends SubRecyclerViewCallBack> extends RecyclerView.Adapter<VH> implements MainRecyclerViewCallBack<Sub> {
private final static int VELOCITY = 1;
private int mSelectedPosition = SELECT_UNKNOWN;
private List mDummySource = new ArrayList();
@Override
public void setDragPosition(int position,boolean shouldNotify) {
if (position >= getItemCount() || position < -1) return;
if (position == -1 && mSelectedPosition != -1) {
int oldPosition = mSelectedPosition;
mSelectedPosition = position;
if(shouldNotify) notifyItemChanged(oldPosition);
} else {
mSelectedPosition = position;
if(shouldNotify) notifyItemChanged(mSelectedPosition);
}
}
@Override
public int getDragPosition() {
return mSelectedPosition;
}
@SuppressWarnings("unchecked")
@Override
public void onDragStart(RecyclerView recyclerView, int position) {
VH selectedViewHolder = (VH) recyclerView.findViewHolderForAdapterPosition(position);
if(selectedViewHolder == null) return;
onDragStart(selectedViewHolder,position);
}
@SuppressWarnings("unchecked")
@Override
public void onDragAnimationEnd(RecyclerView recyclerView, int position) {
VH selectedViewHolder = (VH) recyclerView.findViewHolderForAdapterPosition(position);
if(selectedViewHolder == null) return;
onDragAnimationEnd(selectedViewHolder,position);
}
@SuppressWarnings("unchecked")
@Override
public boolean onMergeStart(RecyclerView parent, int selectedPosition, int targetPosition) {
VH selectedViewHolder = (VH) parent.findViewHolderForAdapterPosition(selectedPosition);
VH targetViewHolder = (VH) parent.findViewHolderForAdapterPosition(targetPosition);
if(selectedViewHolder == null || targetViewHolder == null) return false;
return onMergeStart(selectedViewHolder, targetViewHolder, selectedPosition, targetPosition);
}
@SuppressWarnings("unchecked")
@Override
public void onMergeCancel(RecyclerView parent, int selectedPosition, int targetPosition) {
VH selectedViewHolder = (VH) parent.findViewHolderForAdapterPosition(selectedPosition);
VH targetViewHolder = (VH) parent.findViewHolderForAdapterPosition(targetPosition);
if(selectedViewHolder == null || targetViewHolder == null) return;
onMergeCancel(selectedViewHolder, targetViewHolder, selectedPosition, targetPosition);
}
@SuppressWarnings("unchecked")
@Override
public void onMerged(RecyclerView parent, int selectedPosition, int targetPosition) {
VH selectedViewHolder = (VH) parent.findViewHolderForAdapterPosition(selectedPosition);
VH targetViewHolder = (VH) parent.findViewHolderForAdapterPosition(targetPosition);
if(selectedViewHolder == null || targetViewHolder == null) return;
onMerged(selectedViewHolder, targetViewHolder, selectedPosition, targetPosition);
}
@SuppressWarnings("unchecked")
@Override
public MergeInfo onPrepareMerge(RecyclerView parent, int selectedPosition, int targetPosition) {
VH selectedViewHolder = (VH) parent.findViewHolderForAdapterPosition(selectedPosition);
VH targetViewHolder = (VH) parent.findViewHolderForAdapterPosition(targetPosition);
if(selectedViewHolder == null || targetViewHolder == null) return null;
return onPrePareMerge(selectedViewHolder, targetViewHolder, selectedPosition, targetPosition);
}
@SuppressWarnings("unchecked")
@Override
public void onStartMergeAnimation(RecyclerView parent, int selectedPosition, int targetPosition,int duration) {
VH selectedViewHolder = (VH) parent.findViewHolderForAdapterPosition(selectedPosition);
VH targetViewHolder = (VH) parent.findViewHolderForAdapterPosition(targetPosition);
if(selectedViewHolder == null || targetViewHolder == null) return;
onStartMergeAnimation(selectedViewHolder, targetViewHolder, selectedPosition, targetPosition,duration);
}
public void onDragStart(VH selectedViewHolder,int selectedPosition){
}
public void onDragAnimationEnd(VH selectedViewHolder,int selectedPosition){
}
public abstract boolean onMergeStart(VH selectedViewHolder, VH targetViewHolder, int selectedPosition, int targetPosition);
public abstract void onMergeCancel(VH selectedViewHolder, VH targetViewHolder, int selectedPosition, int targetPosition);
public abstract void onMerged(VH selectedViewHolder, VH targetViewHolder, int selectedPosition, int targetPosition);
public abstract MergeInfo onPrePareMerge(VH selectedViewHolder, VH targetViewHolder, int selectedPosition, int targetPosition);
public abstract void onStartMergeAnimation(VH selectedViewHolder, VH targetViewHolder, int selectedPosition, int targetPosition,int duration);
@Override
public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
if (position == mSelectedPosition) {
holder.itemView.setVisibility(View.INVISIBLE);
} else {
holder.itemView.setVisibility(View.VISIBLE);
}
super.onBindViewHolder(holder, position, payloads);
}
@Override
public boolean canDropOver(int selectedPosition, int targetPosition) {
return true;
}
@Override
public int getCurrentState(View selectedView, View targetView, int x, int y,
VelocityTracker velocityTracker, int selectedPosition,
int targetPosition) {
if (velocityTracker == null) return ClassifyView.MOVE_STATE_NONE;
int left = x;
int top = y;
int right = left + selectedView.getWidth();
int bottom = top + selectedView.getHeight();
if (canMergeItem(selectedPosition, targetPosition)) {
if ((Math.abs(left - targetView.getLeft()) + Math.abs(right - targetView.getRight()) +
Math.abs(top - targetView.getTop()) + Math.abs(bottom - targetView.getBottom()))
< (targetView.getWidth() + targetView.getHeight()
) / 3) {
return ClassifyView.MOVE_STATE_MERGE;
}
}
if ((Math.abs(left - targetView.getLeft()) + Math.abs(right - targetView.getRight()) +
Math.abs(top - targetView.getTop()) + Math.abs(bottom - targetView.getBottom()))
< (targetView.getWidth() + targetView.getHeight()
) / 2) {
velocityTracker.computeCurrentVelocity(100);
float xVelocity = velocityTracker.getXVelocity();
float yVelocity = velocityTracker.getYVelocity();
float limit = getVelocity(targetView.getContext());
if (xVelocity < limit && yVelocity < limit) {
return ClassifyView.MOVE_STATE_MOVE;
}
}
return ClassifyView.MOVE_STATE_NONE;
}
@Override
public boolean canExplodeItem(int position, View pressedView) {
return false;
}
@Override
public List explodeItem(int position, View pressedView) {
return canExplodeItem(position,pressedView)? mDummySource :null;
}
@Override
public float getVelocity(Context context) {
float density = context.getResources().getDisplayMetrics().density;
return density * VELOCITY + .5f;
}
@Override
public void moved(int selectedPosition, int targetPosition) {
}
@Override
public void onItemClick(RecyclerView recyclerView, int position, View pressedView) {
onItemClick(position,pressedView);
}
@Override
public void onItemClick(int position, View pressedView) {
}
}