0

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.!!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It says mGestureDetector is never initialized. How can i initialize it ?
@AndiGeeky the same way you did in your code (in constructor); I didn't paste the constructor part in my snippet for brevity
@ Android777 : Ohk..Thanks.!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.