AndiGeeky
AndiGeeky

Reputation: 11474

Swipe in Scrollview is not working all time

I have one layout in which i have used ScrollView. Now I have also implemented 'CustomScrollView' so that i can filter Swipe action that view.

Here is my custom ScrollView :

public class CustomScrollView extends ScrollView {
    private GestureDetector mGestureDetector;

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    // Return false if we're scrolling in the x direction
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return Math.abs(distanceY) > Math.abs(distanceX);
        }
    }
}

Now I have also implemeted GestureDetector. On swiping view vill call onFling 80/100 times.

Can I have some suggestions so that i can implement more accurate swipe behaviour.?

Thanks.!!

Upvotes: 0

Views: 653

Answers (1)

AndroidEx
AndroidEx

Reputation: 15824

Maybe the problem of onFling() not getting called lies in the fact that CustomScrollView sometimes also intercepts horizontal gestures/scrolling?

I would try using this small class, which was made to better filter out horizontal gestures and then hook up your GestureDetector:

public class CustomScrollView extends ScrollView {

    private GestureDetector mGestureDetector;

    private float mHorizontalDistance;
    private float mVerticalDistance;
    private float mPreviousX;
    private float mPreviousY;

    @Override
    public boolean onInterceptTouchEvent(@NonNull MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mHorizontalDistance = mVerticalDistance = 0f;
            mPreviousX = ev.getX();
            mPreviousY = ev.getY();
            mGestureDetector.onTouchEvent(ev);
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            mHorizontalDistance += Math.abs(curX - mPreviousX);
            mVerticalDistance += Math.abs(curY - mPreviousY);
            mPreviousX = curX;
            mPreviousY = curY;
            if (mHorizontalDistance > mVerticalDistance) {
                mGestureDetector.onTouchEvent(ev);
                return false;
            }
        }

        return super.onInterceptTouchEvent(ev);
    }
}

Combining scrollviews and gestures can be a hard task requiring multiple tweaks and adjustments for a specific case. I'm pretty sure in the filtering part (because it's tried and tested in the production environment), not so sure about the part where the gesture detector comes in, this requires testing.

Upvotes: 1

Related Questions