I have a ListView
, each of whose items is a Horizontal RecyclerView
. The problem that I am facing is, the horizontal scroll is not very smooth. If I scroll/swipe in a perfectly horizontal direction, then it works fine but if the scroll is even slightly non-horizontal (say at a 10 degree angle to horizontal), it regards that as up/down scroll rather than left / right, due to the parent ListView
. I tried this method:
recyclerView.setOnTouchListener(new FlingRecyclerView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
Log.i("Scroll down", "Intercept true");
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_MOVE:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
}
// Handle HorizontalScrollView touch events.
v.onTouchEvent(event);
return true;
}
});
But it was not very effective. In fact, I couldn't notice much improvement. That's because this motionEvent gets called only when the motion is perfectly horizontal - that case is already working fine. Then I tried doing this:
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
Log.i("TAG", "VelocityX " + Float.toString(velocityX) + "VelocityY " + Float.toString(velocityY));
Log.i("TAG", "e1X " + Float.toString(e1.getX()) + "e1Y " + Float.toString(e1.getY()) + "e2X " + Float.toString(e2.getX()) + "e2Y " + Float.toString(e2.getY()));
// downward swipe
if (e2.getY() - e1.getY() > SWIPE_MAX_OFF_PATH && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(TestActivity.this, "Downward Swipe", Toast.LENGTH_SHORT).show();
Log.i("TAG", "Downward swipe");
}
else if (e1.getY() - e2.getY() > SWIPE_MAX_OFF_PATH && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(TestActivity.this, "Upward Swipe", Toast.LENGTH_SHORT).show();
Log.i("TAG", "Upward swipe");
}
// right to left swipe
else if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(TestActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
Log.i("TAG", "Left swipe");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(TestActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
Log.i("TAG", "Right swipe");
}
} catch (Exception e) {
// nothing
}
return false;
}
}
And I noticed that it registers the events only when I release my finger, not when I start the swipe. It does not work as soon as I touch the RecyclerView
. And this too, failed to produce any noticeable improvement. I also noticed one thing that these methods are somehow dependent on pressure too. When I did a swipe with a light pressure, nothing happened. And the same thing done in same path, with greater pressure does produce scroll.
Is there any way to make the scroll smooth even if the path of swipe is not horizontally straight or even if the motion is kind of circular? Any help would be highly appreciated.