Skip to content

Commit 662460d

Browse files
committed
Swipe events added
1 parent a1da8d2 commit 662460d

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.shrikanthravi.collapsiblecalendarview.listener;
2+
3+
import android.content.Context;
4+
import android.view.GestureDetector;
5+
import android.view.GestureDetector.SimpleOnGestureListener;
6+
import android.view.MotionEvent;
7+
import android.view.View;
8+
import android.view.View.OnTouchListener;
9+
10+
public class OnSwipeTouchListener implements OnTouchListener {
11+
12+
private final GestureDetector gestureDetector;
13+
14+
public OnSwipeTouchListener (Context ctx){
15+
gestureDetector = new GestureDetector(ctx, new GestureListener());
16+
}
17+
18+
@Override
19+
public boolean onTouch(View v, MotionEvent event) {
20+
return gestureDetector.onTouchEvent(event);
21+
}
22+
23+
private final class GestureListener extends SimpleOnGestureListener {
24+
25+
private static final int SWIPE_THRESHOLD = 100;
26+
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
27+
28+
@Override
29+
public boolean onDown(MotionEvent e) {
30+
return true;
31+
}
32+
33+
@Override
34+
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
35+
boolean result = false;
36+
try {
37+
float diffY = e2.getY() - e1.getY();
38+
float diffX = e2.getX() - e1.getX();
39+
if (Math.abs(diffX) > Math.abs(diffY)) {
40+
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
41+
if (diffX > 0) {
42+
onSwipeRight();
43+
} else {
44+
onSwipeLeft();
45+
}
46+
result = true;
47+
}
48+
}
49+
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
50+
if (diffY > 0) {
51+
onSwipeBottom();
52+
} else {
53+
onSwipeTop();
54+
}
55+
result = true;
56+
}
57+
} catch (Exception exception) {
58+
exception.printStackTrace();
59+
}
60+
return result;
61+
}
62+
}
63+
64+
public void onSwipeRight() {
65+
}
66+
67+
public void onSwipeLeft() {
68+
}
69+
70+
public void onSwipeTop() {
71+
}
72+
73+
public void onSwipeBottom() {
74+
}
75+
}

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/view/LockScrollView.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import android.content.Context;
44
import android.util.AttributeSet;
55
import android.view.MotionEvent;
6+
import android.view.View;
67
import android.widget.ScrollView;
78

89
/**
910
* Created by shrikanthravi on 07/03/18.
1011
*/
1112

12-
public class LockScrollView extends ScrollView {
13+
public class LockScrollView extends ScrollView {
1314
public LockScrollView(Context context) {
1415
super(context);
1516
}
@@ -24,10 +25,12 @@ public LockScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
2425

2526
@Override
2627
public boolean onInterceptTouchEvent(MotionEvent ev) {
28+
super.onInterceptTouchEvent(ev);
2729
return false;
2830
}
2931
@Override
3032
public boolean onTouchEvent(MotionEvent ev) {
33+
super.onTouchEvent(ev);
3134
return false;
3235
}
3336
}

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/CollapsibleCalendar.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.shrikanthravi.collapsiblecalendarview.data.CalendarAdapter;
2626
import com.shrikanthravi.collapsiblecalendarview.data.Day;
2727
import com.shrikanthravi.collapsiblecalendarview.data.Event;
28+
import com.shrikanthravi.collapsiblecalendarview.listener.OnSwipeTouchListener;
2829
import com.shrikanthravi.collapsiblecalendarview.view.ExpandIconView;
2930

3031
import java.text.SimpleDateFormat;
@@ -71,7 +72,7 @@ protected void init(Context context) {
7172

7273

7374
// bind events
74-
75+
mLayoutRoot.setOnTouchListener(getSwipeTouchListener());
7576
mBtnPrevMonth.setOnClickListener(new View.OnClickListener() {
7677
@Override
7778
public void onClick(View v) {
@@ -126,6 +127,33 @@ public void run() {
126127

127128
}
128129

130+
private OnSwipeTouchListener getSwipeTouchListener() {
131+
return new OnSwipeTouchListener(getContext()) {
132+
public void onSwipeTop() {
133+
collapse(400);
134+
135+
}
136+
public void onSwipeLeft() {
137+
if (getState() == STATE_COLLAPSED)
138+
nextWeek();
139+
else if (getState() == STATE_EXPANDED)
140+
nextMonth();
141+
}
142+
public void onSwipeRight() {
143+
if (getState() == STATE_COLLAPSED) {
144+
prevWeek();
145+
}
146+
else if (getState() == STATE_EXPANDED) {
147+
prevMonth();
148+
}
149+
}
150+
public void onSwipeBottom() {
151+
expand(400);
152+
}
153+
154+
};
155+
}
156+
129157
@Override
130158
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
131159
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
@@ -237,6 +265,7 @@ protected void reload() {
237265
0,
238266
ViewGroup.LayoutParams.WRAP_CONTENT,
239267
1));
268+
view.setOnTouchListener(getSwipeTouchListener());
240269
view.setOnClickListener(new View.OnClickListener() {
241270
@Override
242271
public void onClick(View v) {

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/UICalendar.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.shrikanthravi.collapsiblecalendarview.R;
2121
import com.shrikanthravi.collapsiblecalendarview.data.Day;
22+
import com.shrikanthravi.collapsiblecalendarview.listener.OnSwipeTouchListener;
2223
import com.shrikanthravi.collapsiblecalendarview.view.ExpandIconView;
2324
import com.shrikanthravi.collapsiblecalendarview.view.LockScrollView;
2425

0 commit comments

Comments
 (0)