Created
July 7, 2014 13:20
-
-
Save decnorton/793c8bcf0b72d377a680 to your computer and use it in GitHub Desktop.
Easy quick return. No need for dodgy observable ListView hacks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QuickReturn { | |
private static final String TAG = "QuickReturn"; | |
protected GestureDetectorCompat mGestureDetector; | |
private Context mContext; | |
private View mQuickReturnView; | |
private View mTargetView; | |
public QuickReturn(Context context, View targetView, View quickReturnView) { | |
mContext = context; | |
mQuickReturnView = quickReturnView; | |
mTargetView = targetView; | |
mGestureDetector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
mQuickReturnView.setTranslationY(Math.max(Math.min(mQuickReturnView.getTranslationY() + distanceY, mQuickReturnView.getHeight()), 0)); | |
return false; | |
} | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
// On up | |
if (velocityY > 0) { | |
// Show: Animate to 0 | |
mQuickReturnView.animate().translationY(0).setDuration(100).start(); | |
} else { | |
// Hide: Animate to -height | |
mQuickReturnView.animate().translationY(mQuickReturnView.getHeight()).setDuration(100).start(); | |
} | |
return false; | |
} | |
}); | |
targetView.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) { | |
hideQuickReturnViewIfNeeded(); | |
} | |
return mGestureDetector.onTouchEvent(event); | |
} | |
}); | |
} | |
protected void showQuickReturnView() { | |
// Show: Animate to 0 | |
mQuickReturnView.animate().translationY(0).setDuration(100).start(); | |
} | |
protected void hideQuickReturnViewIfNeeded() { | |
if (mQuickReturnView.getTranslationY() < mQuickReturnView.getHeight() / 2) { | |
showQuickReturnView(); | |
} else { | |
hideQuickReturnView(); | |
} | |
} | |
protected void hideQuickReturnView() { | |
// Hide: Animate to -height | |
mQuickReturnView.animate().translationY(mQuickReturnView.getHeight()).setDuration(100).start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment