Skip to content

Commit 48dc2d1

Browse files
authored
Merge pull request yuyakaido#19 from kndl22/master
Freedom swipe without bottom option added, overlay for both
2 parents 12e58cd + 64331f1 commit 48dc2d1

File tree

8 files changed

+148
-14
lines changed

8 files changed

+148
-14
lines changed

cardstackview/src/main/java/com/yuyakaido/android/cardstackview/CardStackView.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public CardStackView(Context context, AttributeSet attrs, int defStyle) {
9999
setSwipeDirection(SwipeDirection.from(array.getInt(R.styleable.CardStackView_swipeDirection, 0)));
100100
setLeftOverlay(array.getResourceId(R.styleable.CardStackView_leftOverlay, 0));
101101
setRightOverlay(array.getResourceId(R.styleable.CardStackView_rightOverlay, 0));
102+
setBottomOverlay(array.getResourceId(R.styleable.CardStackView_bottomOverlay, 0));
103+
setTopOverlay(array.getResourceId(R.styleable.CardStackView_topOverlay, 0));
102104
array.recycle();
103105
}
104106

@@ -125,7 +127,7 @@ private void initializeViews() {
125127
.inflate(R.layout.card_container, this, false);
126128
view.setDraggable(false);
127129
view.setCardStackOption(option);
128-
view.setOverlay(option.leftOverlay, option.rightOverlay);
130+
view.setOverlay(option.leftOverlay, option.rightOverlay, option.bottomOverlay, option.topOverlay);
129131
containers.add(0, view);
130132
addView(view);
131133
}
@@ -256,6 +258,12 @@ public void performSwipe(SwipeDirection direction, AnimatorSet set, final Animat
256258
} else if (direction == SwipeDirection.Right) {
257259
getTopView().showRightOverlay();
258260
getTopView().setOverlayAlpha(1f);
261+
} else if (direction == SwipeDirection.Bottom){
262+
getTopView().showBottomOverlay();
263+
getTopView().setOverlayAlpha(1f);
264+
} else if (direction == SwipeDirection.Top){
265+
getTopView().showTopOverlay();
266+
getTopView().setOverlayAlpha(1f);
259267
}
260268
set.addListener(listener);
261269
set.start();
@@ -421,6 +429,20 @@ public void setRightOverlay(int rightOverlay) {
421429
}
422430
}
423431

432+
public void setBottomOverlay(int bottomOverlay) {
433+
option.bottomOverlay = bottomOverlay;
434+
if (adapter != null) {
435+
initialize(false);
436+
}
437+
}
438+
439+
public void setTopOverlay(int topOverlay) {
440+
option.topOverlay = topOverlay;
441+
if (adapter != null) {
442+
initialize(false);
443+
}
444+
}
445+
424446
public void setPaginationReserved() {
425447
state.isPaginationReserved = true;
426448
}

cardstackview/src/main/java/com/yuyakaido/android/cardstackview/SwipeDirection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public enum SwipeDirection {
88

99
public static final List<SwipeDirection> FREEDOM = Arrays
1010
.asList(SwipeDirection.values());
11+
public static final List<SwipeDirection> FREEDOM_NO_BOTTOM = Arrays
12+
.asList(SwipeDirection.Top,SwipeDirection.Left, SwipeDirection.Right);
1113
public static final List<SwipeDirection> HORIZONTAL = Arrays
1214
.asList(SwipeDirection.Left, SwipeDirection.Right);
1315
public static final List<SwipeDirection> VERTICAL = Arrays
@@ -18,8 +20,10 @@ public static List<SwipeDirection> from(int value) {
1820
case 0:
1921
return FREEDOM;
2022
case 1:
21-
return HORIZONTAL;
23+
return FREEDOM_NO_BOTTOM;
2224
case 2:
25+
return HORIZONTAL;
26+
case 3:
2327
return VERTICAL;
2428
default:
2529
return FREEDOM;

cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/CardContainerView.java

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class CardContainerView extends FrameLayout {
3232
private ViewGroup overlayContainer = null;
3333
private View leftOverlayView = null;
3434
private View rightOverlayView = null;
35+
private View bottomOverlayView = null;
36+
private View topOverlayView = null;
3537

3638
private ContainerEventListener containerEventListener = null;
3739
private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
@@ -139,7 +141,7 @@ private void handleActionUp(MotionEvent event) {
139141
if (Math.cos(radian) < -0.5) {
140142
direction = SwipeDirection.Left;
141143
} else {
142-
direction = SwipeDirection.Top;
144+
direction = SwipeDirection.Bottom;
143145
}
144146
break;
145147
case BottomRight:
@@ -148,7 +150,7 @@ private void handleActionUp(MotionEvent event) {
148150
radian = Math.toRadians(degree);
149151
if (Math.cos(radian) < 0.5) {
150152
direction = SwipeDirection.Bottom;
151-
} else {
153+
}else{
152154
direction = SwipeDirection.Right;
153155
}
154156
break;
@@ -206,12 +208,24 @@ private void updateRotation() {
206208
}
207209

208210
private void updateAlpha() {
209-
if (getPercentX() < 0) {
210-
showLeftOverlay();
211-
} else {
212-
showRightOverlay();
211+
float percentX = getPercentX();
212+
float percentY = getPercentY();
213+
214+
if (Math.abs(percentX) > Math.abs(percentY)){
215+
if (percentX < 0) {
216+
showLeftOverlay();
217+
} else {
218+
showRightOverlay();
219+
}
220+
setOverlayAlpha(Math.abs(percentX));
221+
}else{
222+
if (percentY < 0) {
223+
showTopOverlay();
224+
} else {
225+
showBottomOverlay();
226+
}
227+
setOverlayAlpha(Math.abs(percentY));
213228
}
214-
setOverlayAlpha(Math.abs(getPercentX()));
215229
}
216230

217231
private void moveToOrigin() {
@@ -250,7 +264,7 @@ public ViewGroup getOverlayContainer() {
250264
return overlayContainer;
251265
}
252266

253-
public void setOverlay(int left, int right) {
267+
public void setOverlay(int left, int right, int bottom, int top) {
254268
if (leftOverlayView != null) {
255269
overlayContainer.removeView(leftOverlayView);
256270
}
@@ -268,6 +282,24 @@ public void setOverlay(int left, int right) {
268282
overlayContainer.addView(rightOverlayView);
269283
ViewCompat.setAlpha(rightOverlayView, 0f);
270284
}
285+
286+
if (bottomOverlayView != null) {
287+
overlayContainer.removeView(bottomOverlayView);
288+
}
289+
if (bottom != 0) {
290+
bottomOverlayView = LayoutInflater.from(getContext()).inflate(bottom, overlayContainer, false);
291+
overlayContainer.addView(bottomOverlayView);
292+
ViewCompat.setAlpha(bottomOverlayView, 0f);
293+
}
294+
295+
if (topOverlayView != null) {
296+
overlayContainer.removeView(topOverlayView);
297+
}
298+
if (top != 0) {
299+
topOverlayView = LayoutInflater.from(getContext()).inflate(top, overlayContainer, false);
300+
overlayContainer.addView(topOverlayView);
301+
ViewCompat.setAlpha(topOverlayView, 0f);
302+
}
271303
}
272304

273305
public void setOverlayAlpha(float alpha) {
@@ -281,17 +313,69 @@ public void showLeftOverlay() {
281313
if (rightOverlayView != null) {
282314
ViewCompat.setAlpha(rightOverlayView, 0f);
283315
}
316+
if (bottomOverlayView != null) {
317+
ViewCompat.setAlpha(bottomOverlayView, 0f);
318+
}
319+
if (topOverlayView != null) {
320+
ViewCompat.setAlpha(topOverlayView, 0f);
321+
}
284322
}
285323

286324
public void showRightOverlay() {
287325
if (leftOverlayView != null) {
288326
ViewCompat.setAlpha(leftOverlayView, 0f);
289327
}
328+
329+
if (bottomOverlayView != null) {
330+
ViewCompat.setAlpha(bottomOverlayView, 0f);
331+
}
332+
333+
if (topOverlayView != null) {
334+
ViewCompat.setAlpha(topOverlayView, 0f);
335+
}
336+
290337
if (rightOverlayView != null) {
291338
ViewCompat.setAlpha(rightOverlayView, 1f);
292339
}
293340
}
294341

342+
public void showBottomOverlay() {
343+
if (leftOverlayView != null) {
344+
ViewCompat.setAlpha(leftOverlayView, 0f);
345+
}
346+
347+
if (bottomOverlayView != null) {
348+
ViewCompat.setAlpha(bottomOverlayView, 1f);
349+
}
350+
351+
if (topOverlayView != null) {
352+
ViewCompat.setAlpha(topOverlayView, 0f);
353+
}
354+
355+
if (rightOverlayView != null) {
356+
ViewCompat.setAlpha(rightOverlayView, 0f);
357+
}
358+
}
359+
360+
361+
public void showTopOverlay() {
362+
if (leftOverlayView != null) {
363+
ViewCompat.setAlpha(leftOverlayView, 0f);
364+
}
365+
366+
if (bottomOverlayView != null) {
367+
ViewCompat.setAlpha(bottomOverlayView, 0f);
368+
}
369+
370+
if (topOverlayView != null) {
371+
ViewCompat.setAlpha(topOverlayView, 1f);
372+
}
373+
374+
if (rightOverlayView != null) {
375+
ViewCompat.setAlpha(rightOverlayView, 0f);
376+
}
377+
}
378+
295379
public float getViewOriginX() {
296380
return viewOriginX;
297381
}

cardstackview/src/main/java/com/yuyakaido/android/cardstackview/internal/CardStackOption.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ public class CardStackOption {
1515
public boolean isSwipeEnabled = true;
1616
public int leftOverlay = 0; // Layout Resource ID
1717
public int rightOverlay = 0; // Layout Resource ID
18+
public int bottomOverlay = 0; // Layout Resource ID
19+
public int topOverlay = 0; // Layout Resource ID
1820
public List<SwipeDirection> swipeDirection = SwipeDirection.FREEDOM;
1921
}

cardstackview/src/main/res/values/attrs.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
<attr name="swipeEnabled" format="boolean"/>
1515
<attr name="swipeDirection" format="enum">
1616
<enum name="freedom" value="0"/>
17-
<enum name="horizontal" value="1"/>
18-
<enum name="vertical" value="2"/>
17+
<enum name="freedom_no_bottom" value="1"/>
18+
<enum name="horizontal" value="2"/>
19+
<enum name="vertical" value="3"/>
1920
</attr>
2021
<attr name="leftOverlay" format="reference"/>
2122
<attr name="rightOverlay" format="reference"/>
23+
<attr name="bottomOverlay" format="reference"/>
24+
<attr name="topOverlay" format="reference"/>
2225
</declare-styleable>
2326
</resources>

sample/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
app:stackFrom="top"
2323
app:elevationEnabled="true"
2424
app:swipeEnabled="true"
25-
app:swipeDirection="horizontal"
25+
app:swipeDirection="freedom_no_bottom"
2626
app:leftOverlay="@layout/overlay_left"
27-
app:rightOverlay="@layout/overlay_right"/>
27+
app:rightOverlay="@layout/overlay_right"
28+
app:topOverlay="@layout/overlay_top" />
2829

2930
<ProgressBar
3031
android:id="@+id/activity_main_progress_bar"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.v7.widget.CardView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
app:cardBackgroundColor="#88000000"
8+
app:cardCornerRadius="8dp"
9+
app:cardElevation="0dp"
10+
app:cardUseCompatPadding="true">
11+
12+
<ImageView
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:layout_gravity="center"
16+
android:src="@mipmap/overlay_top"/>
17+
18+
</android.support.v7.widget.CardView>
Loading

0 commit comments

Comments
 (0)