Search code examples
androidandroid-fragmentsandroid-recyclerviewtouch-eventmotionevent

Change views based on touch events


Let's say I have a list (RecyclerView) with some items, when a user puts her finger on one of those items, I need to show a completely different screen, and based on the position where a user takes her finger off of that different screen, my application does some different actions.

I have managed to achieve something with Fragment transactions (I replace the Fragment containing the RecyclerView with the Fragment containing that different screen when I receive an ACTION_DOWN MotionEvent in one of the list's item Views), but the problem is that the second Fragment cannot receive ACTION_UP or any other MotionEvent because ACTION_DOWN has never been dispatched on it, and I need a way to know when a user takes her finger off.

Is there any way to achieve my desired behavior (maybe with some different technique than replacing Fragments)?


Solution

  • When you return true in onTouchEvent for your view it takes complete control over the rest of the gesture up until and including the ACTION_UP MotionEvent.

    If you get the coordinates of the ACTION_UP from the original listener and then send them to wherever you want to process that information it will do what you want.

    Another technique would be to override onInterceptTouchEvent() within a ViewGroup. This will enable you to intercept the event right after the view item receives the ACTION_DOWN MotionEvent. The rest of that gesture including the ACTION_UP will then be redirected to the viewgroups onTouchEvent().