Detecting horizontal scroll in ListView

I need to define horizontal scrolling as a list. Should I use a gesture detector or onTouch event. I need to support Android 2.1 +

One post states that you need to override onInterceptTouchEvent in a ListView, as shown below:

@Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // reset difference values mDiffX = 0; mDiffY = 0; mLastX = ev.getX(); mLastY = ev.getY(); break; case MotionEvent.ACTION_MOVE: final float curX = ev.getX(); final float curY = ev.getY(); mDiffX += Math.abs(curX - mLastX); mDiffY += Math.abs(curY - mLastY); mLastX = curX; mLastY = curY; // don't intercept event, when user tries to scroll vertically if (mDiffX > mDiffY) { return false; // do not react to horizontal touch events, these events will be passed to your list item view } } return super.onInterceptTouchEvent(ev); } 

Not sure if this works or is this the best way. It didn’t help that the question was lightly closed 8 days ago: Swipe the screen inside ListView - Android

+7
source share

All Articles