-
Notifications
You must be signed in to change notification settings - Fork 57
/
BaseSubAdapter.java
158 lines (127 loc) · 4.71 KB
/
BaseSubAdapter.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
package com.anarchy.classify.adapter;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.VelocityTracker;
import android.view.View;
import com.anarchy.classify.ClassifyView;
import com.anarchy.classify.callback.SubRecyclerViewCallBack;
import java.util.List;
/**
* Version 1.0
* <p>
* Date: 16/6/1 15:34
* Author: rsshinide38@163.com
* <p>
*/
public abstract class BaseSubAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements SubRecyclerViewCallBack {
private final static int VELOCITY = 1;
@Override
public boolean canDragOnLongPress(int position, View pressedView) {
return true;
}
private int mSelectedPosition = SELECT_UNKNOWN;
@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) notifyDataSetChanged();
// notifyItemChanged(oldPosition);
}else {
mSelectedPosition = position;
if(shouldNotify) notifyItemChanged(mSelectedPosition);
}
}
@Override
public void initData(int parentIndex, List data) {
prepareExplodeItem(parentIndex);
}
@Override
public void prepareExplodeItem(int parentPosition) {
}
@Override
public void onDialogShow(Dialog subDialog, int parentPosition) {
}
@Override
public void onDialogCancel(Dialog subDialog, int parentPosition) {
}
@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);
}
public void onDragStart(VH selectedViewHolder,int selectedPosition){
}
public void onDragAnimationEnd(VH selectedViewHolder,int selectedPosition){
}
@Override
public int getDragPosition() {
return mSelectedPosition;
}
@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 boolean canDragOut(int selectedPosition) {
return true;
}
@Override
public void moved(int selectedPosition, int targetPosition) {
}
@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((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 float getVelocity(Context context) {
float density = context.getResources().getDisplayMetrics().density;
return density*VELOCITY + .5f;
}
@Override
public void onItemClick(RecyclerView recyclerView, int position, View pressedView) {
onItemClick(position,pressedView);
}
@Override
public void onItemClick(int position, View pressedView) {
}
}