1

I am developing an app, which contains several fragments just like the picture below:

Picture 1

The bottom view is MainActivity, and there are several fragments above MainActivity, which may contain buttons. I want to add a fragment on top in order to listen the OnTouchEvents (gestures), but ignore the onclick events and pass the onclick events to the fragments below.

Here are my questions,

  1. How can I distinguish onTouchEvent and onclick events in my codes?
  2. How can I pass the onclick events to the fragments below?
    I saw some people suggested to use onInterceptTouchEvent(), but I am not sure if this applies to my case and I do not really understand how to use onInterceptTouchEvent().

Update:
I tried to override boolean dispatchTouchEvent(MotionEvent ev) to make the top fragment ignore the click event, however since the first touch event must be MotionEvent.ACTION_DOWN, it seems that there is no way for me to determine whether the touch event is click.

So, is there any other way to do this?

Tim
  • 123
  • 1
  • 2
  • 11

2 Answers2

0

From the Documentation

The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of a ViewGroup, including on the surface of its children. If onInterceptTouchEvent() returns true, the MotionEvent is intercepted, meaning it will be not be passed on to the child, but rather to the onTouchEvent() method of the parent.

The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its children do. If you return true from onInterceptTouchEvent(), the child view that was previously handling touch events receives an ACTION_CANCEL, and the events from that point forward are sent to the parent's onTouchEvent() method for the usual handling. onInterceptTouchEvent() can also return false and simply spy on events as they travel down the view hierarchy to their usual targets, which will handle the events with their own onTouchEvent().

So, you'll have to return true/false (in onInterceptTouchEvent method) according to your application flow logic. If you use OnTouchListener you can avoid using OnClickListener. See example in the documentation to understand it better.

Community
  • 1
  • 1
lubilis
  • 3,942
  • 4
  • 31
  • 54
0

This will give you the idea how onTouch and onClick are different. You can use custom interfaces to pass data to lower fragment for manipulation

Community
  • 1
  • 1