-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
SwipeBlockableViewPager.java
113 lines (90 loc) · 3.26 KB
/
SwipeBlockableViewPager.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
package com.heinrichreimersoftware.materialintro.view;
import android.content.Context;
import android.support.annotation.IntDef;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class SwipeBlockableViewPager extends ViewPager {
@IntDef({SWIPE_DIRECTION_LEFT, SWIPE_DIRECTION_NONE, SWIPE_DIRECTION_RIGHT})
@Retention(RetentionPolicy.SOURCE)
private @interface SwipeDirection {
}
private static final int SWIPE_DIRECTION_LEFT = 1;
private static final int SWIPE_DIRECTION_NONE = 0;
private static final int SWIPE_DIRECTION_RIGHT = -1;
private static final int SWIPE_THRESHOLD = 0;
private float initialX;
private boolean swipeRightEnabled = true;
private boolean swipeLeftEnabled = true;
private boolean locked = false;
public SwipeBlockableViewPager(Context context) {
super(context);
}
public SwipeBlockableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setSwipeRightEnabled(boolean swipeRightEnabled) {
this.swipeRightEnabled = swipeRightEnabled;
}
public void setSwipeLeftEnabled(boolean swipeLeftEnabled) {
this.swipeLeftEnabled = swipeLeftEnabled;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return handleTouchEvent(event) && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return handleTouchEvent(event) && super.onInterceptTouchEvent(event);
}
private boolean handleTouchEvent(MotionEvent event) {
if (getSwipeDirection(event) == SWIPE_DIRECTION_RIGHT && !swipeRightEnabled) {
if (!locked) {
updatePosition();
locked = true;
}
return false;
}
else if (getSwipeDirection(event) == SWIPE_DIRECTION_LEFT && !swipeLeftEnabled) {
if (!locked) {
updatePosition();
locked = true;
}
return false;
}
locked = false;
return true;
}
private void updatePosition() {
int currentItem = getCurrentItem();
scrollTo(currentItem * getWidth(), getScrollY());
PagerAdapter adapter = getAdapter();
if (adapter != null) {
adapter.notifyDataSetChanged();
}
setCurrentItem(currentItem);
}
@SwipeDirection
public int getSwipeDirection(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
initialX = event.getX();
return SWIPE_DIRECTION_NONE;
}
if (event.getAction() == MotionEvent.ACTION_MOVE ||
event.getAction() == MotionEvent.ACTION_UP) {
float distanceX = event.getX() - initialX;
if (SWIPE_THRESHOLD > Math.abs(distanceX)) {
return SWIPE_DIRECTION_NONE;
}
if (distanceX > 0) {
return SWIPE_DIRECTION_RIGHT;
} else {
return SWIPE_DIRECTION_LEFT;
}
}
return SWIPE_DIRECTION_NONE;
}
}