|  | @@ -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,8 @@ 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 = 200;
 | 
	
		
			
			|  | 17 | +
 | 
	
		
			
			| 15 | 18 |      public enum Direction {
 | 
	
		
			
			| 16 | 19 |          Up, Down, None
 | 
	
		
			
			| 17 | 20 |      }
 | 
	
	
		
			
			|  | @@ -33,11 +36,14 @@ public class CollapseCalculator {
 | 
	
		
			
			| 33 | 36 |      private float totalCollapse = 0;
 | 
	
		
			
			| 34 | 37 |      private float totalCollapseDeltaSinceTouchDown = 0;
 | 
	
		
			
			| 35 | 38 |      private final int scaledTouchSlop;
 | 
	
		
			
			|  | 39 | +    private final int minimumFlingVelocity;
 | 
	
		
			
			| 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 vc = ViewConfiguration.get(NavigationApplication.instance);
 | 
	
		
			
			|  | 45 | +        scaledTouchSlop = vc.getScaledTouchSlop();
 | 
	
		
			
			|  | 46 | +        minimumFlingVelocity = vc.getScaledMinimumFlingVelocity();
 | 
	
		
			
			| 41 | 47 |          setFlingDetector();
 | 
	
		
			
			| 42 | 48 |      }
 | 
	
		
			
			| 43 | 49 |  
 | 
	
	
		
			
			|  | @@ -46,13 +52,30 @@ public class CollapseCalculator {
 | 
	
		
			
			| 46 | 52 |              flingDetector =
 | 
	
		
			
			| 47 | 53 |                      new GestureDetector(NavigationApplication.instance, new GestureDetector.SimpleOnGestureListener() {
 | 
	
		
			
			| 48 | 54 |                          @Override
 | 
	
		
			
			| 49 |  | -                        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
 | 
	
		
			
			|  | 55 | +                        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, final float velocityY) {
 | 
	
		
			
			| 50 | 56 |                              final Direction direction = getScrollDirection(e1, e2);
 | 
	
		
			
			| 51 |  | -                            if (canCollapse(direction) && totalCollapse != 0) {
 | 
	
		
			
			|  | 57 | +                            final float diff = Math.abs(e2.getRawY() - e1.getRawY());
 | 
	
		
			
			|  | 58 | +
 | 
	
		
			
			|  | 59 | +                            if (Math.abs(velocityY) <  minimumFlingVelocity || diff < FLING_DISTANCE_PIXELS_THRESHOLD) {
 | 
	
		
			
			|  | 60 | +                                Log.w("FLING", "Consuming fling v: [" + velocityY + "] dy: [" + diff + "]");
 | 
	
		
			
			|  | 61 | +                                return true;
 | 
	
		
			
			|  | 62 | +                            }
 | 
	
		
			
			|  | 63 | +
 | 
	
		
			
			|  | 64 | +                            if (canCollapse && totalCollapse != 0) {
 | 
	
		
			
			| 52 | 65 |                                  flingListener.onFling(new CollapseAmount(direction));
 | 
	
		
			
			|  | 66 | +                                if (direction == Direction.Up) {
 | 
	
		
			
			|  | 67 | +                                    view.asView().postOnAnimation(new Runnable() {
 | 
	
		
			
			|  | 68 | +                                        @Override
 | 
	
		
			
			|  | 69 | +                                        public void run() {
 | 
	
		
			
			|  | 70 | +                                            Log.v("FLING", "v: [" + velocityY + "] dy: [" + diff + "]");
 | 
	
		
			
			|  | 71 | +                                            scrollView.fling((int) Math.abs(velocityY));
 | 
	
		
			
			|  | 72 | +                                        }
 | 
	
		
			
			|  | 73 | +                                    });
 | 
	
		
			
			|  | 74 | +                                }
 | 
	
		
			
			| 53 | 75 |                                  return true;
 | 
	
		
			
			| 54 | 76 |                              }
 | 
	
		
			
			| 55 |  | -                            return false;
 | 
	
		
			
			|  | 77 | +
 | 
	
		
			
			|  | 78 | +                            return true;
 | 
	
		
			
			| 56 | 79 |                          }
 | 
	
		
			
			| 57 | 80 |  
 | 
	
		
			
			| 58 | 81 |                          private Direction getScrollDirection(MotionEvent e1, MotionEvent e2) {
 | 
	
	
		
			
			|  | @@ -76,7 +99,8 @@ public class CollapseCalculator {
 | 
	
		
			
			| 76 | 99 |      @NonNull
 | 
	
		
			
			| 77 | 100 |      CollapseAmount calculate(MotionEvent event) {
 | 
	
		
			
			| 78 | 101 |          updateInitialTouchY(event);
 | 
	
		
			
			| 79 |  | -        CollapseAmount touchUpCollapse = shouldCollapseOnTouchUp(event);
 | 
	
		
			
			|  | 102 | +        final boolean isFling = flingDetector.onTouchEvent(event);
 | 
	
		
			
			|  | 103 | +        CollapseAmount touchUpCollapse = shouldCollapseOnTouchUp(event, isFling);
 | 
	
		
			
			| 80 | 104 |          if (touchUpCollapse != CollapseAmount.None) {
 | 
	
		
			
			| 81 | 105 |              previousTouchEvent = MotionEvent.obtain(event);
 | 
	
		
			
			| 82 | 106 |              return touchUpCollapse;
 | 
	
	
		
			
			|  | @@ -97,9 +121,8 @@ public class CollapseCalculator {
 | 
	
		
			
			| 97 | 121 |          }
 | 
	
		
			
			| 98 | 122 |      }
 | 
	
		
			
			| 99 | 123 |  
 | 
	
		
			
			| 100 |  | -    private CollapseAmount shouldCollapseOnTouchUp(MotionEvent event) {
 | 
	
		
			
			| 101 |  | -        if ((collapseBehaviour.shouldCollapseOnTouchUp())
 | 
	
		
			
			| 102 |  | -            && !flingDetector.onTouchEvent(event) && isTouchUp(event)) {
 | 
	
		
			
			|  | 124 | +    private CollapseAmount shouldCollapseOnTouchUp(MotionEvent event, boolean isFling) {
 | 
	
		
			
			|  | 125 | +        if (isTouchUp(event) && collapseBehaviour.shouldCollapseOnTouchUp() && !isFling) {
 | 
	
		
			
			| 103 | 126 |              final float visibilityPercentage = view.getCurrentCollapseValue() / view.getFinalCollapseValue();
 | 
	
		
			
			| 104 | 127 |              Direction direction = visibilityPercentage >= 0.5f ? Direction.Up : Direction.Down;
 | 
	
		
			
			| 105 | 128 |              if (canCollapse(direction) && totalCollapse != 0) {
 |