35
35
public class CustomViewAbove extends ViewGroup {
36
36
37
37
private static final String TAG = "CustomViewAbove" ;
38
- private static final boolean DEBUG = false ;
38
+ private static final boolean DEBUG = true ;
39
39
40
40
private static final boolean USE_CACHE = false ;
41
41
@@ -86,7 +86,7 @@ public float getInterpolation(float t) {
86
86
protected int mMaximumVelocity ;
87
87
private int mFlingDistance ;
88
88
89
- private CustomViewBehind mCustomViewBehind ;
89
+ private CustomViewBehind mViewBehind ;
90
90
// private int mMode;
91
91
private boolean mEnabled = true ;
92
92
@@ -154,10 +154,6 @@ public CustomViewAbove(Context context) {
154
154
}
155
155
156
156
public CustomViewAbove (Context context , AttributeSet attrs ) {
157
- this (context , attrs , true );
158
- }
159
-
160
- public CustomViewAbove (Context context , AttributeSet attrs , boolean isAbove ) {
161
157
super (context , attrs );
162
158
initCustomViewAbove ();
163
159
}
@@ -174,14 +170,14 @@ void initCustomViewAbove() {
174
170
mMaximumVelocity = configuration .getScaledMaximumFlingVelocity ();
175
171
setInternalPageChangeListener (new SimpleOnPageChangeListener () {
176
172
public void onPageSelected (int position ) {
177
- if (mCustomViewBehind != null ) {
173
+ if (mViewBehind != null ) {
178
174
switch (position ) {
179
175
case 0 :
180
176
case 2 :
181
- mCustomViewBehind .setChildrenEnabled (true );
177
+ mViewBehind .setChildrenEnabled (true );
182
178
break ;
183
179
case 1 :
184
- mCustomViewBehind .setChildrenEnabled (false );
180
+ mViewBehind .setChildrenEnabled (false );
185
181
break ;
186
182
}
187
183
}
@@ -227,7 +223,7 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int
227
223
return ;
228
224
}
229
225
230
- item = mCustomViewBehind .getMenuPage (item );
226
+ item = mViewBehind .getMenuPage (item );
231
227
232
228
final boolean dispatchSelected = mCurItem != item ;
233
229
mCurItem = item ;
@@ -312,19 +308,19 @@ public int getDestScrollX(int page) {
312
308
switch (page ) {
313
309
case 0 :
314
310
case 2 :
315
- return mCustomViewBehind .getMenuLeft (mContent , page );
311
+ return mViewBehind .getMenuLeft (mContent , page );
316
312
case 1 :
317
313
return mContent .getLeft ();
318
314
}
319
315
return 0 ;
320
316
}
321
317
322
318
private int getLeftBound () {
323
- return mCustomViewBehind .getAbsLeftBound (mContent );
319
+ return mViewBehind .getAbsLeftBound (mContent );
324
320
}
325
321
326
322
private int getRightBound () {
327
- return mCustomViewBehind .getAbsRightBound (mContent );
323
+ return mViewBehind .getAbsRightBound (mContent );
328
324
}
329
325
330
326
public int getContentLeft () {
@@ -345,10 +341,10 @@ private boolean isInIgnoredView(MotionEvent ev) {
345
341
}
346
342
347
343
public int getBehindWidth () {
348
- if (mCustomViewBehind == null ) {
344
+ if (mViewBehind == null ) {
349
345
return 0 ;
350
346
} else {
351
- return mCustomViewBehind .getBehindWidth ();
347
+ return mViewBehind .getBehindWidth ();
352
348
}
353
349
}
354
350
@@ -446,7 +442,7 @@ public View getContent() {
446
442
}
447
443
448
444
public void setCustomViewBehind (CustomViewBehind cvb ) {
449
- mCustomViewBehind = cvb ;
445
+ mViewBehind = cvb ;
450
446
}
451
447
452
448
@ Override
@@ -477,7 +473,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
477
473
@ Override
478
474
protected void onLayout (boolean changed , int l , int t , int r , int b ) {
479
475
final int width = r - l ;
480
- final int height = b - t ;
476
+ final int height = b - t ;
481
477
mContent .layout (0 , 0 , width , height );
482
478
}
483
479
@@ -580,15 +576,15 @@ public int getTouchMode() {
580
576
private boolean thisTouchAllowed (MotionEvent ev ) {
581
577
int x = (int ) (ev .getX () + mScrollX );
582
578
if (isMenuOpen ()) {
583
- return mCustomViewBehind .menuOpenTouchAllowed (mContent , mCurItem , x );
579
+ return mViewBehind .menuOpenTouchAllowed (mContent , mCurItem , x );
584
580
} else {
585
581
switch (mTouchMode ) {
586
582
case SlidingMenu .TOUCHMODE_FULLSCREEN :
587
583
return !isInIgnoredView (ev );
588
584
case SlidingMenu .TOUCHMODE_NONE :
589
585
return false ;
590
586
case SlidingMenu .TOUCHMODE_MARGIN :
591
- return mCustomViewBehind .marginTouchAllowed (mContent , x );
587
+ return mViewBehind .marginTouchAllowed (mContent , x );
592
588
}
593
589
}
594
590
return false ;
@@ -597,9 +593,9 @@ private boolean thisTouchAllowed(MotionEvent ev) {
597
593
private boolean thisSlideAllowed (float dx ) {
598
594
boolean allowed = false ;
599
595
if (isMenuOpen ()) {
600
- allowed = mCustomViewBehind .menuOpenSlideAllowed (dx );
596
+ allowed = mViewBehind .menuOpenSlideAllowed (dx );
601
597
} else {
602
- allowed = mCustomViewBehind .menuClosedSlideAllowed (dx );
598
+ allowed = mViewBehind .menuClosedSlideAllowed (dx );
603
599
}
604
600
if (DEBUG )
605
601
Log .v (TAG , "this slide allowed " + allowed + " dx: " + dx );
@@ -613,6 +609,8 @@ private int getPointerIndex(MotionEvent ev, int id) {
613
609
return activePointerIndex ;
614
610
}
615
611
612
+ private boolean mQuickReturn = false ;
613
+
616
614
@ Override
617
615
public boolean onInterceptTouchEvent (MotionEvent ev ) {
618
616
@@ -635,19 +633,16 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
635
633
final int activePointerId = mActivePointerId ;
636
634
if (activePointerId == INVALID_POINTER )
637
635
break ;
638
-
639
636
final int pointerIndex = this .getPointerIndex (ev , activePointerId );
640
- if (mActivePointerId == INVALID_POINTER )
641
- break ;
642
637
final float x = MotionEventCompat .getX (ev , pointerIndex );
643
638
final float dx = x - mLastMotionX ;
644
639
final float xDiff = Math .abs (dx );
645
640
final float y = MotionEventCompat .getY (ev , pointerIndex );
646
641
final float yDiff = Math .abs (y - mLastMotionY );
647
- if (DEBUG ) Log .v (TAG , "onInterceptTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + "), mLastMotionX:" + mLastMotionX );
642
+ // if (DEBUG) Log.v(TAG, "onInterceptTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + "), mLastMotionX:" + mLastMotionX);
648
643
if (xDiff > mTouchSlop && xDiff > yDiff && thisSlideAllowed (dx )) {
649
644
if (DEBUG ) Log .v (TAG , "Starting drag! from onInterceptTouch" );
650
- mIsBeingDragged = true ;
645
+ startDrag () ;
651
646
mLastMotionX = x ;
652
647
setScrollingCacheEnabled (true );
653
648
} else if (yDiff > mTouchSlop ) {
@@ -663,8 +658,9 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
663
658
if (thisTouchAllowed (ev )) {
664
659
mIsBeingDragged = false ;
665
660
mIsUnableToDrag = false ;
666
- if (isMenuOpen ())
667
- return true ;
661
+ if (isMenuOpen () && mViewBehind .menuTouchInQuickReturn (mContent , mCurItem , ev .getX () + mScrollX )) {
662
+ mQuickReturn = true ;
663
+ }
668
664
} else {
669
665
mIsUnableToDrag = true ;
670
666
}
@@ -680,8 +676,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
680
676
}
681
677
mVelocityTracker .addMovement (ev );
682
678
}
683
-
684
- return mIsBeingDragged ;
679
+ return mIsBeingDragged || mQuickReturn ;
685
680
}
686
681
687
682
@@ -691,7 +686,10 @@ public boolean onTouchEvent(MotionEvent ev) {
691
686
if (!mEnabled )
692
687
return false ;
693
688
694
- if (!mIsBeingDragged && !thisTouchAllowed (ev ))
689
+ // if (!mIsBeingDragged && !thisTouchAllowed(ev))
690
+ // return false;
691
+
692
+ if (!mIsBeingDragged && !mQuickReturn )
695
693
return false ;
696
694
697
695
final int action = ev .getAction ();
@@ -714,22 +712,24 @@ public boolean onTouchEvent(MotionEvent ev) {
714
712
mActivePointerId = MotionEventCompat .getPointerId (ev , 0 );
715
713
break ;
716
714
case MotionEvent .ACTION_MOVE :
717
- if (!mIsBeingDragged ) {
718
- final int pointerIndex = getPointerIndex (ev , mActivePointerId );
719
- if (mActivePointerId == INVALID_POINTER ) {
715
+ if (!mIsBeingDragged ) {
716
+ if (mActivePointerId == INVALID_POINTER )
720
717
break ;
721
- }
718
+ final int pointerIndex = getPointerIndex ( ev , mActivePointerId );
722
719
final float x = MotionEventCompat .getX (ev , pointerIndex );
723
720
final float dx = x - mLastMotionX ;
724
721
final float xDiff = Math .abs (dx );
725
722
final float y = MotionEventCompat .getY (ev , pointerIndex );
726
723
final float yDiff = Math .abs (y - mLastMotionY );
727
- if (DEBUG ) Log .v (TAG , "onTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + "), mLastMotionX:" + mLastMotionX );
728
- if (xDiff > mTouchSlop && xDiff > yDiff && thisSlideAllowed (dx )) {
724
+ // if (DEBUG) Log.v(TAG, "onTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + ")\nmIsBeingDragged:" + mIsBeingDragged + " , mLastMotionX:" + mLastMotionX);
725
+ if (( xDiff > mTouchSlop || mQuickReturn ) && xDiff > yDiff && thisSlideAllowed (dx )) {
729
726
if (DEBUG ) Log .v (TAG , "Starting drag! from onTouch" );
730
- mIsBeingDragged = true ;
727
+ startDrag () ;
731
728
mLastMotionX = x ;
732
729
setScrollingCacheEnabled (true );
730
+ } else {
731
+ if (DEBUG ) Log .v (TAG , "onTouch returning false" );
732
+ return false ;
733
733
}
734
734
}
735
735
if (mIsBeingDragged ) {
@@ -763,8 +763,8 @@ public boolean onTouchEvent(MotionEvent ev) {
763
763
int initialVelocity = (int ) VelocityTrackerCompat .getXVelocity (
764
764
velocityTracker , mActivePointerId );
765
765
final int scrollX = getScrollX ();
766
- // final int widthWithMargin = getWidth();
767
- // final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
766
+ // final int widthWithMargin = getWidth();
767
+ // final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
768
768
// TODO test this. should get better flinging behavior
769
769
final float pageOffset = (float ) (scrollX - getDestScrollX (mCurItem )) / getBehindWidth ();
770
770
final int activePointerIndex = getPointerIndex (ev , mActivePointerId );
@@ -778,9 +778,10 @@ public boolean onTouchEvent(MotionEvent ev) {
778
778
}
779
779
mActivePointerId = INVALID_POINTER ;
780
780
endDrag ();
781
- } else if (isMenuOpen ( )) {
781
+ } else if (mQuickReturn && mViewBehind . menuTouchInQuickReturn ( mContent , mCurItem , ev . getX () + mScrollX )) {
782
782
// close the menu
783
783
setCurrentItem (1 );
784
+ endDrag ();
784
785
}
785
786
break ;
786
787
case MotionEvent .ACTION_CANCEL :
@@ -813,9 +814,9 @@ public void scrollTo(int x, int y) {
813
814
super .scrollTo (x , y );
814
815
mScrollX = x ;
815
816
if (mEnabled )
816
- mCustomViewBehind .scrollBehindTo (mContent , x , y );
817
- // if (mSelectorDrawable != null)
818
- // invalidate();
817
+ mViewBehind .scrollBehindTo (mContent , x , y );
818
+ // if (mSelectorDrawable != null)
819
+ // invalidate();
819
820
}
820
821
821
822
private int determineTargetPage (float pageOffset , int velocity , int deltaX ) {
@@ -833,68 +834,21 @@ private int determineTargetPage(float pageOffset, int velocity, int deltaX) {
833
834
}
834
835
835
836
protected float getPercentOpen () {
836
- return Math .abs (mScrollX ) / getBehindWidth ();
837
+ if (DEBUG ) Log .v (TAG , "" + Math .abs (mScrollX -mContent .getLeft ()) / getBehindWidth ());
838
+ return Math .abs (mScrollX -mContent .getLeft ()) / getBehindWidth ();
837
839
}
838
840
839
841
@ Override
840
842
protected void dispatchDraw (Canvas canvas ) {
841
843
super .dispatchDraw (canvas );
842
844
// Draw the margin drawable if needed.
843
- mCustomViewBehind .drawShadow (mContent , canvas );
844
-
845
- mCustomViewBehind .drawFade (mContent , canvas , getPercentOpen ());
846
- // if (mSelectorEnabled)
847
- // onDrawMenuSelector(canvas, getPercentOpen());
845
+ mViewBehind .drawShadow (mContent , canvas );
846
+ mViewBehind .drawFade (mContent , canvas , getPercentOpen ());
847
+ mViewBehind .drawSelector (mContent , canvas , getPercentOpen ());
848
848
}
849
849
850
850
// variables for drawing
851
851
private float mScrollX = 0.0f ;
852
- // for the indicator
853
- private boolean mSelectorEnabled = true ;
854
- private Bitmap mSelectorDrawable ;
855
- private View mSelectedView ;
856
-
857
- private void onDrawMenuSelector (Canvas canvas , float openPercent ) {
858
- if (mSelectorDrawable != null && mSelectedView != null ) {
859
- String tag = (String ) mSelectedView .getTag (R .id .selected_view );
860
- if (tag .equals (TAG +"SelectedView" )) {
861
- int right = getContentLeft ();
862
- int left = (int ) (right - mSelectorDrawable .getWidth () * openPercent );
863
-
864
- canvas .save ();
865
- canvas .clipRect (left , 0 , right , getHeight ());
866
- canvas .drawBitmap (mSelectorDrawable , left , getSelectedTop (), null );
867
- canvas .restore ();
868
- }
869
- }
870
- }
871
-
872
- public void setSelectorEnabled (boolean b ) {
873
- mSelectorEnabled = b ;
874
- }
875
-
876
- public void setSelectedView (View v ) {
877
- if (mSelectedView != null ) {
878
- mSelectedView .setTag (R .id .selected_view , null );
879
- mSelectedView = null ;
880
- }
881
- if (v .getParent () != null ) {
882
- mSelectedView = v ;
883
- mSelectedView .setTag (R .id .selected_view , TAG +"SelectedView" );
884
- invalidate ();
885
- }
886
- }
887
-
888
- private int getSelectedTop () {
889
- int y = mSelectedView .getTop ();
890
- y += (mSelectedView .getHeight () - mSelectorDrawable .getHeight ()) / 2 ;
891
- return y ;
892
- }
893
-
894
- public void setSelectorBitmap (Bitmap b ) {
895
- mSelectorDrawable = b ;
896
- refreshDrawableState ();
897
- }
898
852
899
853
private void onSecondaryPointerUp (MotionEvent ev ) {
900
854
if (DEBUG ) Log .v (TAG , "onSecondaryPointerUp called" );
@@ -912,7 +866,13 @@ private void onSecondaryPointerUp(MotionEvent ev) {
912
866
}
913
867
}
914
868
869
+ private void startDrag () {
870
+ mIsBeingDragged = true ;
871
+ mQuickReturn = false ;
872
+ }
873
+
915
874
private void endDrag () {
875
+ mQuickReturn = false ;
916
876
mIsBeingDragged = false ;
917
877
mIsUnableToDrag = false ;
918
878
mActivePointerId = INVALID_POINTER ;
@@ -1021,7 +981,7 @@ public boolean arrowScroll(int direction) {
1021
981
direction );
1022
982
if (nextFocused != null && nextFocused != currentFocused ) {
1023
983
if (direction == View .FOCUS_LEFT ) {
1024
- handled = nextFocused .requestFocus ();
984
+ handled = nextFocused .requestFocus ();
1025
985
} else if (direction == View .FOCUS_RIGHT ) {
1026
986
// If there is nothing to the right, or this is causing us to
1027
987
// jump to the left, then what we really want to do is page right.
0 commit comments