Skip to content

Instantly share code, notes, and snippets.

@GokhanArik
Last active September 24, 2019 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GokhanArik/f080556f979888f3f891cb4fbc29f3d7 to your computer and use it in GitHub Desktop.
Save GokhanArik/f080556f979888f3f891cb4fbc29f3d7 to your computer and use it in GitHub Desktop.
Mimics behavior of Bottom Sheet in Android. From initial position, scrolling up disabled. If user scrolls more than half of view's height, view collapses. Otherwise, it goes back to its old position.
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.OvershootInterpolator;
public class BottomSheetDragDownListener implements View.OnTouchListener {
private static final float INTERPOLATOR_FACTOR = 0.5f;
private float dY, startScrollY, viewY;
@Override
public boolean onTouch(View view, MotionEvent event) {
if (view == null){
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startScrollY = event.getRawY();
viewY = view.getY();
dY = startScrollY - viewY;
break;
case MotionEvent.ACTION_MOVE:
if (event.getRawY() > startScrollY) {
view.animate()
.y(event.getRawY() - dY)
.setDuration(0)
.start();
}
break;
case MotionEvent.ACTION_UP:
if (event.getRawY() > startScrollY) {
if ((event.getRawY() - startScrollY) < view.getHeight() / 2) {
view.animate()
.y(viewY)
.setInterpolator(new OvershootInterpolator(INTERPOLATOR_FACTOR))
.setDuration(200)
.start();
} else {
view.animate()
.y(viewY + view.getHeight())
.setDuration(200)
.setInterpolator(new OvershootInterpolator(INTERPOLATOR_FACTOR))
.start();
}
}
default:
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment