You could try to use the onTouchEvent
and GestureDetector
to detect the drag, then use the onBackPress()
:
public class StackAct1 extends AppCompatActivity {
protected static final float FLIP_DISTANCE = 50;
GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stack);
mDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) { return false; }
@Override
public void onShowPress(MotionEvent e) {}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; }
@Override
public void onLongPress(MotionEvent e) { }
/**
*
* e1 The first down motion event that started the fling. e2 The
* move motion event that triggered the current onFling.
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > FLIP_DISTANCE) {
Log.i("MYTAG", "Slide left...");
return true;
}
if (e2.getX() - e1.getX() > FLIP_DISTANCE) {
Log.i("MYTAG", "Slide right...");
return true;
}
if (e1.getY() - e2.getY() > FLIP_DISTANCE) {
Log.i("MYTAG", "Slide up...");
return true;
}
if (e2.getY() - e1.getY() > FLIP_DISTANCE) {
Log.i("MYTAG", "Slide down...");
onBackPressed();
return true;
}
Log.d("TAG", e2.getX() + " " + e2.getY());
return false;
}
@Override
public boolean onDown(MotionEvent e) { return false; }
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mDetector.onTouchEvent(event);
}}
Slide down in the UI of activity, it will run the onBackPress()
.