|
@@ -2,6 +2,7 @@ package com.reactnativenavigation.views.collapsingToolbar;
|
2
|
2
|
|
3
|
3
|
import android.support.annotation.NonNull;
|
4
|
4
|
import android.support.annotation.Nullable;
|
|
5
|
+import android.util.Log;
|
5
|
6
|
import android.view.GestureDetector;
|
6
|
7
|
import android.view.MotionEvent;
|
7
|
8
|
import android.view.ViewConfiguration;
|
|
@@ -12,6 +13,9 @@ import com.reactnativenavigation.views.collapsingToolbar.behaviours.CollapseBeha
|
12
|
13
|
import com.reactnativenavigation.views.collapsingToolbar.behaviours.TitleBarHideOnScrollBehaviour;
|
13
|
14
|
|
14
|
15
|
public class CollapseCalculator {
|
|
16
|
+ private static final int FLING_DISTANCE_PIXELS_THRESHOLD = 100;
|
|
17
|
+ private static final int FLING_VELOCITY = 7000;
|
|
18
|
+
|
15
|
19
|
public enum Direction {
|
16
|
20
|
Up, Down, None
|
17
|
21
|
}
|
|
@@ -32,12 +36,12 @@ public class CollapseCalculator {
|
32
|
36
|
private int scrollY = 0;
|
33
|
37
|
private float totalCollapse = 0;
|
34
|
38
|
private float totalCollapseDeltaSinceTouchDown = 0;
|
35
|
|
- private final int scaledTouchSlop;
|
|
39
|
+ private final ViewConfiguration viewConfiguration;
|
36
|
40
|
|
37
|
41
|
public CollapseCalculator(final CollapsingView collapsingView, CollapseBehaviour collapseBehaviour) {
|
38
|
42
|
this.view = collapsingView;
|
39
|
43
|
this.collapseBehaviour = collapseBehaviour;
|
40
|
|
- scaledTouchSlop = ViewConfiguration.get(NavigationApplication.instance).getScaledTouchSlop();
|
|
44
|
+ viewConfiguration = ViewConfiguration.get(NavigationApplication.instance);
|
41
|
45
|
setFlingDetector();
|
42
|
46
|
}
|
43
|
47
|
|
|
@@ -46,10 +50,27 @@ public class CollapseCalculator {
|
46
|
50
|
flingDetector =
|
47
|
51
|
new GestureDetector(NavigationApplication.instance, new GestureDetector.SimpleOnGestureListener() {
|
48
|
52
|
@Override
|
49
|
|
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
|
53
|
+ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, final float velocityY) {
|
50
|
54
|
final Direction direction = getScrollDirection(e1, e2);
|
|
55
|
+ final float diff = Math.abs(e2.getRawY() - e1.getRawY());
|
|
56
|
+ if (Math.abs(velocityY) < viewConfiguration.getScaledMinimumFlingVelocity() ||
|
|
57
|
+ diff < FLING_DISTANCE_PIXELS_THRESHOLD) {
|
|
58
|
+ Log.w("FLING", "Consuming fling v: [" + velocityY + "] dy: [" + diff + "]");
|
|
59
|
+ return false;
|
|
60
|
+ }
|
|
61
|
+
|
51
|
62
|
if (canCollapse(direction) && totalCollapse != 0) {
|
52
|
63
|
flingListener.onFling(new CollapseAmount(direction));
|
|
64
|
+ if (direction == Direction.Up) {
|
|
65
|
+ scrollView.postDelayed(new Runnable() {
|
|
66
|
+ @Override
|
|
67
|
+ public void run() {
|
|
68
|
+ int max = (int) Math.max(Math.abs(velocityY), FLING_VELOCITY);
|
|
69
|
+ scrollView.fling(max);
|
|
70
|
+ }
|
|
71
|
+ }, ViewCollapser.FLING_DURATION);
|
|
72
|
+ }
|
|
73
|
+
|
53
|
74
|
return true;
|
54
|
75
|
}
|
55
|
76
|
return false;
|
|
@@ -76,7 +97,8 @@ public class CollapseCalculator {
|
76
|
97
|
@NonNull
|
77
|
98
|
CollapseAmount calculate(MotionEvent event) {
|
78
|
99
|
updateInitialTouchY(event);
|
79
|
|
- CollapseAmount touchUpCollapse = shouldCollapseOnTouchUp(event);
|
|
100
|
+ final boolean isFling = flingDetector.onTouchEvent(event);
|
|
101
|
+ CollapseAmount touchUpCollapse = shouldCollapseOnTouchUp(event, isFling);
|
80
|
102
|
if (touchUpCollapse != CollapseAmount.None) {
|
81
|
103
|
previousTouchEvent = MotionEvent.obtain(event);
|
82
|
104
|
return touchUpCollapse;
|
|
@@ -97,9 +119,8 @@ public class CollapseCalculator {
|
97
|
119
|
}
|
98
|
120
|
}
|
99
|
121
|
|
100
|
|
- private CollapseAmount shouldCollapseOnTouchUp(MotionEvent event) {
|
101
|
|
- if ((collapseBehaviour.shouldCollapseOnTouchUp())
|
102
|
|
- && !flingDetector.onTouchEvent(event) && isTouchUp(event)) {
|
|
122
|
+ private CollapseAmount shouldCollapseOnTouchUp(MotionEvent event, boolean isFling) {
|
|
123
|
+ if (isTouchUp(event) && collapseBehaviour.shouldCollapseOnTouchUp() && !isFling) {
|
103
|
124
|
final float visibilityPercentage = view.getCurrentCollapseValue() / view.getFinalCollapseValue();
|
104
|
125
|
Direction direction = visibilityPercentage >= 0.5f ? Direction.Up : Direction.Down;
|
105
|
126
|
if (canCollapse(direction) && totalCollapse != 0) {
|
|
@@ -195,7 +216,7 @@ public class CollapseCalculator {
|
195
|
216
|
totalCollapseDeltaSinceTouchDown += Math.abs(y - previousCollapseY);
|
196
|
217
|
previousCollapseY = y;
|
197
|
218
|
previousTouchEvent = MotionEvent.obtain(event);
|
198
|
|
- return totalCollapseDeltaSinceTouchDown < scaledTouchSlop ? CollapseAmount.None : new CollapseAmount(collapse);
|
|
219
|
+ return totalCollapseDeltaSinceTouchDown < viewConfiguration.getScaledTouchSlop() ? CollapseAmount.None : new CollapseAmount(collapse);
|
199
|
220
|
}
|
200
|
221
|
|
201
|
222
|
private float calculateCollapse(float y) {
|