-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy
334 lines (264 loc) · 10.8 KB
/
copy
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.lorentzos.flingswipe;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Adapter;
import android.widget.FrameLayout;
/**
* Created by dionysis_lorentzos on 5/8/14
* for package com.lorentzos.swipecards
* and project Swipe cards.
* Use with caution dinosaurs might appear!
*/
public class SwipeFlingAdapterView extends BaseFlingAdapterView {
private int MAX_VISIBLE = 4;
private int MIN_ADAPTER_STACK = 6;
private float ROTATION_DEGREES = 15.f;
private Adapter mAdapter;
private int LAST_OBJECT_IN_STACK = 0;
private onFlingListener mFlingListener;
private AdapterDataSetObserver mDataSetObserver;
private boolean mInLayout = false;
private View mActiveCard = null;
private OnItemClickListener mOnItemClickListener;
private FlingCardListener flingCardListener;
private PointF mLastTouchPoint;
public SwipeFlingAdapterView(Context context) {
this(context, null);
}
public SwipeFlingAdapterView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.SwipeFlingStyle);
}
public SwipeFlingAdapterView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeFlingAdapterView, defStyle, 0);
MAX_VISIBLE = a.getInt(R.styleable.SwipeFlingAdapterView_max_visible, MAX_VISIBLE);
MIN_ADAPTER_STACK = a.getInt(R.styleable.SwipeFlingAdapterView_min_adapter_stack, MIN_ADAPTER_STACK);
ROTATION_DEGREES = a.getFloat(R.styleable.SwipeFlingAdapterView_rotation_degrees, ROTATION_DEGREES);
a.recycle();
}
/**
* A shortcut method to set both the listeners and the adapter.
*
* @param context The activity context which extends onFlingListener, OnItemClickListener or both
* @param mAdapter The adapter you have to set.
*/
public void init(final Context context, Adapter mAdapter) {
if(context instanceof onFlingListener) {
mFlingListener = (onFlingListener) context;
}else{
throw new RuntimeException("Activity does not implement SwipeFlingAdapterView.onFlingListener");
}
if(context instanceof OnItemClickListener){
mOnItemClickListener = (OnItemClickListener) context;
}
setAdapter(mAdapter);
}
@Override
public View getSelectedView() {
return mActiveCard;
}
@Override
public void requestLayout() {
if (!mInLayout) {
super.requestLayout();
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// if we don't have an adapter, we don't need to do anything
if (mAdapter == null) {
return;
}
mInLayout = true;
final int adapterCount = mAdapter.getCount();
if(adapterCount == 0) {
removeAllViewsInLayout();
}else {
View topCard = getChildAt(LAST_OBJECT_IN_STACK);
if(mActiveCard!=null && topCard!=null && topCard==mActiveCard) {
if (this.flingCardListener.isTouching()) {
PointF lastPoint = this.flingCardListener.getLastPoint();
if (this.mLastTouchPoint == null || !this.mLastTouchPoint.equals(lastPoint)) {
this.mLastTouchPoint = lastPoint;
removeViewsInLayout(0, LAST_OBJECT_IN_STACK);
layoutChildren(1, adapterCount);
}
}
}else{
// Reset the UI and set top view listener
removeAllViewsInLayout();
layoutChildren(0, adapterCount);
setTopView();
}
}
mInLayout = false;
if(adapterCount <= MIN_ADAPTER_STACK) mFlingListener.onAdapterAboutToEmpty(adapterCount);
}
private void layoutChildren(int startingIndex, int adapterCount){
while (startingIndex < Math.min(adapterCount, MAX_VISIBLE) ) {
View newUnderChild = mAdapter.getView(startingIndex, null, this);
if (newUnderChild.getVisibility() != GONE) {
makeAndAddView(newUnderChild);
LAST_OBJECT_IN_STACK = startingIndex;
}
startingIndex++;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void makeAndAddView(View child) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) child.getLayoutParams();
addViewInLayout(child, 0, lp, true);
final boolean needToMeasure = child.isLayoutRequested();
if (needToMeasure) {
int childWidthSpec = getChildMeasureSpec(getWidthMeasureSpec(),
getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin,
lp.width);
int childHeightSpec = getChildMeasureSpec(getHeightMeasureSpec(),
getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin,
lp.height);
child.measure(childWidthSpec, childHeightSpec);
} else {
cleanupLayoutState(child);
}
int w = child.getMeasuredWidth();
int h = child.getMeasuredHeight();
int gravity = lp.gravity;
if (gravity == -1) {
gravity = Gravity.TOP | Gravity.START;
}
int layoutDirection = getLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
int childLeft;
int childTop;
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
childLeft = (getWidth() + getPaddingLeft() - getPaddingRight() - w) / 2 +
lp.leftMargin - lp.rightMargin;
break;
case Gravity.END:
childLeft = getWidth() + getPaddingRight() - w - lp.rightMargin;
break;
case Gravity.START:
default:
childLeft = getPaddingLeft() + lp.leftMargin;
break;
}
switch (verticalGravity) {
case Gravity.CENTER_VERTICAL:
childTop = (getHeight() + getPaddingTop() - getPaddingBottom() - h) / 2 +
lp.topMargin - lp.bottomMargin;
break;
case Gravity.BOTTOM:
childTop = getHeight() - getPaddingBottom() - h - lp.bottomMargin;
break;
case Gravity.TOP:
default:
childTop = getPaddingTop() + lp.topMargin;
break;
}
child.layout(childLeft, childTop, childLeft + w, childTop + h);
}
/**
* Set the top view and add the fling listener
*/
private void setTopView() {
if(getChildCount()>0){
mActiveCard = getChildAt(LAST_OBJECT_IN_STACK);
if(mActiveCard!=null) {
flingCardListener = new FlingCardListener(mActiveCard, mAdapter.getItem(0),
ROTATION_DEGREES, new FlingCardListener.FlingListener() {
@Override
public void onCardExited() {
mActiveCard = null;
mFlingListener.removeFirstObjectInAdapter();
}
@Override
public void leftExit(Object dataObject) {
mFlingListener.onLeftCardExit(dataObject);
}
@Override
public void rightExit(Object dataObject) {
mFlingListener.onRightCardExit(dataObject);
}
@Override
public void onClick(Object dataObject) {
if(mOnItemClickListener!=null)
mOnItemClickListener.onItemClicked(0, dataObject);
}
@Override
public void onScroll(float scrollProgressPercent) {
mFlingListener.onScroll(scrollProgressPercent);
}
});
mActiveCard.setOnTouchListener(flingCardListener);
}
}
}
public FlingCardListener getTopCardListener() throws NullPointerException{
if(flingCardListener==null){
throw new NullPointerException();
}
return flingCardListener;
}
public void setMaxVisible(int MAX_VISIBLE){
this.MAX_VISIBLE = MAX_VISIBLE;
}
public void setMinStackInAdapter(int MIN_ADAPTER_STACK){
this.MIN_ADAPTER_STACK = MIN_ADAPTER_STACK;
}
@Override
public Adapter getAdapter() {
return mAdapter;
}
@Override
public void setAdapter(Adapter adapter) {
if (mAdapter != null && mDataSetObserver != null) {
mAdapter.unregisterDataSetObserver(mDataSetObserver);
mDataSetObserver = null;
}
mAdapter = adapter;
if (mAdapter != null && mDataSetObserver == null) {
mDataSetObserver = new AdapterDataSetObserver();
mAdapter.registerDataSetObserver(mDataSetObserver);
}
}
public void setFlingListener(onFlingListener onFlingListener) {
this.mFlingListener = onFlingListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.mOnItemClickListener = onItemClickListener;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new FrameLayout.LayoutParams(getContext(), attrs);
}
private class AdapterDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
requestLayout();
}
@Override
public void onInvalidated() {
requestLayout();
}
}
public interface OnItemClickListener {
void onItemClicked(int itemPosition, Object dataObject);
}
public interface onFlingListener {
void removeFirstObjectInAdapter();
void onLeftCardExit(Object dataObject);
void onRightCardExit(Object dataObject);
void onAdapterAboutToEmpty(int itemsInAdapter);
void onScroll(float scrollProgressPercent);
}
}