Sending touch event to sibling view

I developed my own layout that extends ViewGroup. Now I want to use it in my work. In my work, I have both this layout and the viewer inside the frame. My custom layout fills my frame layout and is above my viewPager.

I would like to be able to handle click events on my own layout and allow all other movements to go to the viewer so that it can still scroll.

I have not done it yet. Either I have a click, but the viewer can no longer scroll, or vice versa. I overridden onTouchEvent and onInterceptTouchEvent.

What I notice is that I get the down event correctly on my custom layout, but as soon as it is caught by the viewer, I never get up. How can I communicate between neighbor views for touch events?

PS: I tried splitMotionEvent to no avail

+2
source share
2 answers

make sure either onTouchEvent or dispatchTouchEvent your custom layout returns true in ACTION_DOWN , and then your own layout will receive ACTION_MOVE and ACTION_UP later.

+2
source

I had the same issue when I tracked OnTouchClickListener and scrolling using OnTouchClickListener .

I solved this by sending a touch to the parent element and the view itself in order to ignore the upcoming event (so that it would be called / called by its native contact / click). If the event should not be ignored, use it.

The code is as follows:

 touchTrackingView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // do not consume this event so the sibling gets the touch/click if (mIgnoreNextTouchEvent) { mIgnoreNextTouchEvent = false; return false; } // TODO do what you need to do with the touch // next touch should be ignored so that // the sibling gets their touch/click invoked mIgnoreNextTouchEvent = true; ((View) v.getParent()).dispatchTouchEvent(event); return true; } }); 
0
source

Source: https://habr.com/ru/post/980028/


All Articles