Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing touch events to the parent view

I have a custom ViewSwitcher in which I implemented touch events so I am able to scroll through screens using the touchscreen.

My layout hierarchy looks like this:

<ViewSwitcher>      <LinearLayout>         <ListView />     </LinearLayout>      <LinearLayout>         <ListView />     </LinearLayout>  </ViewSwitcher> 

Now, the problem is that the touch events are being consumed by the ListViews and I am not able to switch the views. It works fine when I don't have the ListViews. I need to be able to scroll through the views and scroll the ListView.

How do I solve this?

EDIT: I also need the ListView items to be clickable.

Thanks in advance!

like image 259
Srichand Yella Avatar asked Jun 16 '11 15:06

Srichand Yella


People also ask

How to intercept touch event Android?

Intercept touch events in a ViewGroup. The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of a ViewGroup , including on the surface of its children.

What are the touch events and gestures in mobile application?

Android supports a range of touch gestures such as tap, double-tap, pinch, swipe, scroll, long press, drag, and fling.

How are Android touch events delivered?

Touch events are delivered first to Activity. dispatchTouchEvent. It's where you may catch them first. Here they get dispatched to Window, where they traverse View hierarchy, in such order that Widgets that are drawn last (on top of other widgets) have chance to process touch in View.

Which method you should override to control your touch action in Android?

1.1. You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.


1 Answers

Thank you everyone for answering the question. But I was able to figure it out in a much simpler manner. Since my ViewSwitcher wasn't detecting the touch event, I intercepted the touch event, called the onTouchEvent() and returned false. Here:

@Override public boolean onInterceptTouchEvent(MotionEvent ev) {     onTouchEvent(ev);     return false; } 

By overriding the onInterceptTouchEvent(), I was able to intercept the touch event in the activity. Then I called the onTouchEvent() in the ViewSwitcher which handles the switching of the ListViews. And finally by returning false, it makes sure that the ViewGroup doesn't consume the event.

like image 195
Srichand Yella Avatar answered Sep 19 '22 15:09

Srichand Yella