Reputation: 1051
I have developped a custom layout that extends ViewGroup. Now, I want to use it in an activity. In my activity, I have both this layout and a viewpager inside a framelayout. My custom layout fills my Frame layout and is above my viewPager.
I would like to be able to handle click events on my custom layout and let all the other motions go to the viewpager so it can still scroll.
I haven't managed so far. Either I have the click but the viewpager cannot scroll anymore, or the contrary. I have overriden onTouchEvent and onInterceptTouchEvent.
What I notice is that, I correctly receive the down event on my custom layout, but once it has been catched by the viewpager, I do never get up there. How can I make communications between sibling views for touch events ?
PS : I have tried splitMotionEvent to no avail
Upvotes: 4
Views: 3662
Reputation: 6555
I've had the same issue where I were tracking the tap and swipe using OnTouchClickListener
.
I solved it by dispatching the touch to the parent and the view itself to ignore the upcoming event (so that its sibling's touch/click gets called). If the event is not to be ignored, consume it.
The code looks like this:
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;
}
});
Upvotes: 2
Reputation: 101
make sure either the onTouchEvent
or the dispatchTouchEvent
of your custom layout returns true at the ACTION_DOWN
happens, and then your custom layout will recieve the ACTION_MOVE
and ACTION_UP
later.
Upvotes: 3