I have a listview inside a RelativeLayout. I want any clicks on the listview to call the Relativelayout's onClick listener. How can the listview pass it's click events to the parent view.
I tried this not working.
listView.setOnTouchListener(new OnTouchListener() {
// Setting on Touch Listener for handling the touch inside
// ScrollView
#Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of
// child view
listView.requestDisallowInterceptTouchEvent(false);
return false;
}
});
I think you need to use one of those methods in order to be able to intercept the event before it gets sent to the appropriate components:
Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.
ViewGroup.onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views.
ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).'
for reference please check here EventHandlers
Can use in XML:
android:focusable="false"
In java code use:
v.setClickable(false)
Related
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// why this code can be executed.
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
getParent().requestDisallowInterceptTouchEvent(true) tell parent don't intercept touch event. but if parent already intercept touch event, the event can't pass to child view, and child view how to execute getParent().requestDisallowInterceptTouchEvent(true) ?
I will give you a simple example to why to use.
getParent().requestDisallowInterceptTouchEvent()
If i have a ViewPager and Each page is a ScrollableView(Horizontal Scrolling).Now there is a condition to scroll and changing the page of ViewPager using swipe.
i.e View will get confused to scroll or swipe!!!
So this condition need's to be checked in the child View (i.e ScrollableView),once if condition is matched use scrolling and set below
getParent().requestDisallowInterceptTouchEvent(true)
As you don't want any parent View (i.e ViewPager) to interfere in child event.So Basically what the framework will do is won't call onintercepttouchevent() of any of the parent view Reference
But if you want ViewPager to get Event's set below
getParent().requestDisallowInterceptTouchEvent(false)
NOTE: By default on each ACTION_DOWN call of touch Event's requestDisallowInterceptTouchEvent is set to false
parent and childs have their default touch events
I want to pass touch event to parent, while touch for several part of child
need to work the touch event of child,I mean the standard touch events of child(webview) should be called I load in webView youtube soundCloud and when the part of soundCloud or youtube is touched it's default events should be called (start/stop/...)
I pass touch events to parent by
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;}
while I still need to have control of some parts of child view touch events
I'm using a RelativeLayout to overlap two different views. Both Views fill the entire Screen.
If the user touches the screen, I want both views to receive the TouchEvent and to excecute their own onTouch-Methods. At the moment, only one view receives the TouchEvent
How can I make both views receive the TouchEvent?
Thank you in advance for your help.
EDIT: Here's the code
touchView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
view1.onTouchEvent(event);
view2.onTouchEvent(event);
return false;
}
});
You can create a third invisible View and place it on top of the overlapping part. Then in its TouchEvent perform the actions of those two TouchEvents
For top view assign clickable=false
it will allow the touch to intercept through it
You could try to get to check if the rawX & rawY of the TouchEvent are within the hitRect of the view that didn't get the event. If it did, you can use dispatchTouchEvent(MotionEvent) to send the TouchEvent tot the view that didn't get the event
I have a custom linearlayout in which I have two gridviews.
I want to have multitouch on the complete linearlayout, ie, first touch can be in one gridview and second touch can be in another gridview and the linear layout should be able to get multitouch events for this (ACTION_DOWN and ACTION_POINTER_DOWN). I am able to get this by overriding onInterceptTouchEvent in my custom layout.
Now I also want the gridview to scroll independently if required. Although gridview is a scrollable view, it is not able to do so, because I have intercepted the 'down' event. I see that when I try to scroll two events are generated : 'ACTION_DOWN' and 'ACTION_MOVE'.
Since I need ACTION_DOWN for multitouch use case I want it to be sent to linearlayout as well.
Is there a possibility to send the ACTION_DOWN event to both the linearlayout as well as my child view which is gridView in this case?
Need help.
You don't have to intercept ACTION_DOWN just to monitor that it flowed through your container view. In other words, you can override onInterceptTouchEvent() to monitor the touch events before they are passed to the appropriate child view by the framework, but as long as you don't screw with the return value it won't actually steal subsequent touch events from the grid elements. For example:
public boolean onInterceptTouchEvent (MotionEvent ev)
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
//Touchdown!
}
//Don't modify this and touch interaction will happen normally
return super.onInterceptTouchEvent(ev);
}
This allows you to see the event without taking it away from the child element(s).
How do I block onTouchEvent from a view's siblings? I have a ViewGroup with a custom button in it. When the button receives an OnTouchEvent, I want to block further OnTouchEvents from going to the button's siblings. I do want to continue receiving OnTouchEvent in the button.
I think you should use a custom ViewGroup as well. In order to get what you want you should override the onInterceptTouchEvent() method, like stated here:
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().
Basically, according to your own logic (button onTouchEvent, etc), you should instruct your ViewGroup.onInterceptTouchEvent() to return true if MotionEvent raw coords are not included in the visible rect of your button.