How to fine tune swipe with a scroll view in Android. Swipes and scrolling seem to conflict

740 views Asked by At

I have code written with multiple activities currently moving between them with buttons. I want to be able to swipe instead. I have scrollviews with textviews on each activity and when adding swipe code (such as found in Detect swipe gesture when there's a ScrollView Android), I experience conflicts between swiping and scrolling. Sometimes a scroll gesture is mistaken for a swipe and the other way around. How do I fine tune this problem. Here is some of my code:

public class Activity extends AppCompatActivity {

private GestureDetectorCompat gestureObject;
    private float x1,x2;
    static final int MIN_DISTANCE = 300;



protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
      gestureObject = new GestureDetectorCompat(this, new StageOneSlideOverviewActivity.LearnGesture());

}
    @Override
    public boolean dispatchTouchEvent(MotionEvent event){
        this.onTouchEvent(event);
        return super.dispatchTouchEvent(event);
    }

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureObject.onTouchEvent(event);
    boolean swapped = false;


    if(swapped){
        swapped = false;
        return super.onTouchEvent(event);
    }
    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        x1 = event.getX();
    } else if (action == MotionEvent.ACTION_UP) {
        x2 = event.getX();
        float deltaX = x2 - x1;
        if (Math.abs(deltaX) > MIN_DISTANCE) {
            Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show();
            swapped = true;
        } else {

        }
    }
    return super.onTouchEvent(event);

}


class LearnGesture extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {

        if (event2.getX() < event1.getX()) {

            Intent intent = new Intent(StageOneSlideOverviewActivity.this, StageOneSlideOneActivity.class);

            startActivity(intent);
        } else if (event2.getX() > event1.getX()) {

            Intent intent = new Intent(StageOneSlideOverviewActivity.this, StageOneActivity.class);
            //finish(); //finish is used to stop history for mainActivity class
            startActivity(intent);

        }
        return true;
    }

Also what is the MIN_DISTANCE parameter and what does it do? Thanks very much

0

There are 0 answers