-
Notifications
You must be signed in to change notification settings - Fork 184
/
CardStack.java
280 lines (231 loc) · 8.75 KB
/
CardStack.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
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
package com.wenchao.cardstack;
import android.content.res.TypedArray;
import java.util.ArrayList;
import java.util.Queue;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.database.DataSetObserver;
public class CardStack extends RelativeLayout {
private int mColor = -1;
private int mIndex = 0;
private int mNumVisible = 4;
private ArrayAdapter<?> mAdapter;
private OnTouchListener mOnTouchListener;
private CardAnimator mCardAnimator;
//private Queue<View> mIdleStack = new Queue<View>;
private CardEventListener mEventListener = new DefaultStackEventListener(300);
private int mContentResource = 0;
public interface CardEventListener{
//section
// 0 | 1
//--------
// 2 | 3
// swipe distance, most likely be used with height and width of a view ;
boolean swipeEnd(int section,float distance);
boolean swipeStart(int section,float distance);
boolean swipeContinue(int section, float distanceX,float distanceY );
void discarded(int mIndex, int direction);
void topCardTapped();
}
public void discardTop(final int direction){
mCardAnimator.discard(direction, new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator arg0) {
mCardAnimator.initLayout();
mIndex++;
mEventListener.discarded(mIndex, direction);
//mIndex = mIndex%mAdapter.getCount();
loadLast();
viewCollection.get(0).setOnTouchListener(null);
viewCollection.get(viewCollection.size() - 1).setOnTouchListener(mOnTouchListener);
}
});
}
public int getCurrIndex(){
//sync?
return mIndex;
}
//only necessary when I need the attrs from xml, this will be used when inflating layout
public CardStack(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CardStack);
mColor = array.getColor(R.styleable.CardStack_backgroundColor, mColor);
array.recycle();
}
//String sMyValue = attrs.getAttributeValue( "http://schemas.android.com/apk/res/android", "padding" );
//get attrs assign minVisiableNum
for(int i = 0; i<mNumVisible; i++){
addContainerViews();
}
setupAnimation();
}
private void addContainerViews(){
FrameLayout v = new FrameLayout(getContext());
viewCollection.add(v);
addView(v);
}
public void setStackMargin(int margin){
mCardAnimator.setStackMargin(margin);
mCardAnimator.initLayout();
}
public void setContentResource(int res){
mContentResource = res;
}
public void reset(boolean resetIndex){
if(resetIndex) mIndex = 0;
removeAllViews();
viewCollection.clear();
for(int i = 0; i<mNumVisible; i++){
addContainerViews();
}
setupAnimation();
loadData();
}
public void setVisibleCardNum(int visiableNum){
mNumVisible = visiableNum;
reset(false);
}
public void setThreshold(int t){
mEventListener = new DefaultStackEventListener(t);
}
public void setListener(CardEventListener cel){
mEventListener = cel;
}
private void setupAnimation(){
final View cardView = viewCollection.get(viewCollection.size()-1);
mCardAnimator = new CardAnimator(viewCollection, mColor);
mCardAnimator.initLayout();
final DragGestureDetector dd = new DragGestureDetector(CardStack.this.getContext(),new DragGestureDetector.DragListener(){
@Override
public boolean onDragStart(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
mCardAnimator.drag(e1,e2,distanceX,distanceY);
return true;
}
@Override
public boolean onDragContinue(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
float x1 = e1.getRawX();
float y1 = e1.getRawY();
float x2 = e2.getRawX();
float y2 = e2.getRawY();
//float distance = CardUtils.distance(x1,y1,x2,y2);
final int direction = CardUtils.direction(x1,y1,x2,y2);
mCardAnimator.drag(e1,e2,distanceX,distanceY);
mEventListener.swipeContinue(direction, Math.abs(x2-x1),Math.abs(y2-y1));
return true;
}
@Override
public boolean onDragEnd(MotionEvent e1, MotionEvent e2) {
//reverse(e1,e2);
float x1 = e1.getRawX();
float y1 = e1.getRawY();
float x2 = e2.getRawX();
float y2 = e2.getRawY();
float distance = CardUtils.distance(x1,y1,x2,y2);
final int direction = CardUtils.direction(x1,y1,x2,y2);
boolean discard = mEventListener.swipeEnd(direction, distance);
if(discard){
mCardAnimator.discard(direction, new AnimatorListenerAdapter(){
@Override
public void onAnimationEnd(Animator arg0) {
mCardAnimator.initLayout();
mIndex++;
mEventListener.discarded(mIndex,direction);
//mIndex = mIndex%mAdapter.getCount();
loadLast();
viewCollection.get(0).setOnTouchListener(null);
viewCollection.get(viewCollection.size()-1)
.setOnTouchListener(mOnTouchListener);
}
});
}else{
mCardAnimator.reverse(e1,e2);
}
return true;
}
@Override
public boolean onTapUp() {
mEventListener.topCardTapped();
return true;
}
}
);
mOnTouchListener = new OnTouchListener() {
private static final String DEBUG_TAG = "MotionEvents";
@Override
public boolean onTouch(View arg0, MotionEvent event) {
dd.onTouchEvent(event);
return true;
}
};
cardView.setOnTouchListener(mOnTouchListener);
}
private DataSetObserver mOb = new DataSetObserver(){
@Override
public void onChanged(){
reset(false);
}
};
//ArrayList
ArrayList<View> viewCollection = new ArrayList<View>();
public CardStack(Context context) {
super(context);
}
public void setAdapter(final ArrayAdapter<?> adapter){
if(mAdapter != null){
mAdapter.unregisterDataSetObserver(mOb);
}
mAdapter = adapter;
adapter.registerDataSetObserver(mOb);
loadData();
}
public ArrayAdapter getAdapter() {
return mAdapter;
}
private void loadData(){
for(int i=mNumVisible-1 ; i>=0 ; i--) {
ViewGroup parent = (ViewGroup) viewCollection.get(i);
int index = (mIndex + mNumVisible - 1) - i;
if (index > mAdapter.getCount() - 1) {
parent.setVisibility(View.GONE);
}else{
View child = mAdapter.getView(index, getContentView(), this);
parent.addView(child);
parent.setVisibility(View.VISIBLE);
}
}
}
private View getContentView(){
View contentView = null;
if(mContentResource != 0) {
LayoutInflater lf = LayoutInflater.from(getContext());
contentView = lf.inflate(mContentResource,null);
}
return contentView;
}
private void loadLast(){
ViewGroup parent = (ViewGroup)viewCollection.get(0);
int lastIndex = (mNumVisible - 1)+ mIndex;
if(lastIndex > mAdapter.getCount() -1 ){
parent.setVisibility(View.GONE);
return;
}
View child = mAdapter.getView( lastIndex, getContentView(), parent);
parent.removeAllViews();
parent.addView(child);
}
public int getStackSize() {
return mNumVisible;
}
}