I have a ViewPager
which holds a RecyclerView
I add a OnTouchListener
to an image view inside the RecyclerView
's item, trying to detect touches using GestureDetector.SimpleOnGestureListener
but I can't get the onFling
events. I suspect it's happening because either the RecyclerView
or the ViewPager
catching the motion event. How do I fix it?
My code (though it's pretty simple):
public class MyClickListener extends GestureDetector.SimpleOnGestureListener{
public MyClickListener(){
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("aaa", "show press");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("aaa", "tapup");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
Log.d("aaa", "scroll");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("aaa", "long press");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Log.d("aaa", "fling");
return false;
}
setting the oun touch:
public VoiceItemHolder(View view, GestureDetector.OnGestureListener listener) {
super(view);
icon = (CircleButton) view.findViewById(R.id.icon_iv);
touchListener = new GestureDetectorCompat(view.getContext(), listener);
icon.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (touchListener != null)
touchListener.onTouchEvent(event);
return false;
}
Feeling a bit awkward to answer my own question :)
If you have a view in a view group and you want to get onFling
's on the inner view you should override the onInterceptTouchEvent
of the view group and return false