|
@@ -2,7 +2,6 @@ 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;
|
6
|
5
|
import android.view.GestureDetector;
|
7
|
6
|
import android.view.MotionEvent;
|
8
|
7
|
import android.view.ViewConfiguration;
|
|
@@ -13,9 +12,6 @@ import com.reactnativenavigation.views.collapsingToolbar.behaviours.CollapseBeha
|
13
|
12
|
import com.reactnativenavigation.views.collapsingToolbar.behaviours.TitleBarHideOnScrollBehaviour;
|
14
|
13
|
|
15
|
14
|
public class CollapseCalculator {
|
16
|
|
- private static final int FLING_DISTANCE_PIXELS_THRESHOLD = 100;
|
17
|
|
- private static final int FLING_VELOCITY = 7000;
|
18
|
|
-
|
19
|
15
|
public enum Direction {
|
20
|
16
|
Up, Down, None
|
21
|
17
|
}
|
|
@@ -36,12 +32,12 @@ public class CollapseCalculator {
|
36
|
32
|
private int scrollY = 0;
|
37
|
33
|
private float totalCollapse = 0;
|
38
|
34
|
private float totalCollapseDeltaSinceTouchDown = 0;
|
39
|
|
- private final ViewConfiguration viewConfiguration;
|
|
35
|
+ private final int scaledTouchSlop;
|
40
|
36
|
|
41
|
37
|
public CollapseCalculator(final CollapsingView collapsingView, CollapseBehaviour collapseBehaviour) {
|
42
|
38
|
this.view = collapsingView;
|
43
|
39
|
this.collapseBehaviour = collapseBehaviour;
|
44
|
|
- viewConfiguration = ViewConfiguration.get(NavigationApplication.instance);
|
|
40
|
+ scaledTouchSlop = ViewConfiguration.get(NavigationApplication.instance).getScaledTouchSlop();
|
45
|
41
|
setFlingDetector();
|
46
|
42
|
}
|
47
|
43
|
|
|
@@ -50,27 +46,10 @@ public class CollapseCalculator {
|
50
|
46
|
flingDetector =
|
51
|
47
|
new GestureDetector(NavigationApplication.instance, new GestureDetector.SimpleOnGestureListener() {
|
52
|
48
|
@Override
|
53
|
|
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, final float velocityY) {
|
|
49
|
+ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
54
|
50
|
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
|
|
-
|
62
|
51
|
if (canCollapse(direction) && totalCollapse != 0) {
|
63
|
52
|
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
|
|
-
|
74
|
53
|
return true;
|
75
|
54
|
}
|
76
|
55
|
return false;
|
|
@@ -97,8 +76,7 @@ public class CollapseCalculator {
|
97
|
76
|
@NonNull
|
98
|
77
|
CollapseAmount calculate(MotionEvent event) {
|
99
|
78
|
updateInitialTouchY(event);
|
100
|
|
- final boolean isFling = flingDetector.onTouchEvent(event);
|
101
|
|
- CollapseAmount touchUpCollapse = shouldCollapseOnTouchUp(event, isFling);
|
|
79
|
+ CollapseAmount touchUpCollapse = shouldCollapseOnTouchUp(event);
|
102
|
80
|
if (touchUpCollapse != CollapseAmount.None) {
|
103
|
81
|
previousTouchEvent = MotionEvent.obtain(event);
|
104
|
82
|
return touchUpCollapse;
|
|
@@ -119,8 +97,9 @@ public class CollapseCalculator {
|
119
|
97
|
}
|
120
|
98
|
}
|
121
|
99
|
|
122
|
|
- private CollapseAmount shouldCollapseOnTouchUp(MotionEvent event, boolean isFling) {
|
123
|
|
- if (isTouchUp(event) && collapseBehaviour.shouldCollapseOnTouchUp() && !isFling) {
|
|
100
|
+ private CollapseAmount shouldCollapseOnTouchUp(MotionEvent event) {
|
|
101
|
+ if ((collapseBehaviour.shouldCollapseOnTouchUp())
|
|
102
|
+ && !flingDetector.onTouchEvent(event) && isTouchUp(event)) {
|
124
|
103
|
final float visibilityPercentage = view.getCurrentCollapseValue() / view.getFinalCollapseValue();
|
125
|
104
|
Direction direction = visibilityPercentage >= 0.5f ? Direction.Up : Direction.Down;
|
126
|
105
|
if (canCollapse(direction) && totalCollapse != 0) {
|
|
@@ -216,7 +195,7 @@ public class CollapseCalculator {
|
216
|
195
|
totalCollapseDeltaSinceTouchDown += Math.abs(y - previousCollapseY);
|
217
|
196
|
previousCollapseY = y;
|
218
|
197
|
previousTouchEvent = MotionEvent.obtain(event);
|
219
|
|
- return totalCollapseDeltaSinceTouchDown < viewConfiguration.getScaledTouchSlop() ? CollapseAmount.None : new CollapseAmount(collapse);
|
|
198
|
+ return totalCollapseDeltaSinceTouchDown < scaledTouchSlop ? CollapseAmount.None : new CollapseAmount(collapse);
|
220
|
199
|
}
|
221
|
200
|
|
222
|
201
|
private float calculateCollapse(float y) {
|