Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle touch events in ScrollView Android

I would like to implement drag-n-drop feature for Android 2.2. I override onTouchListener for each my control. But all my controls are inside ScrollView. When all my controls are visible on in ScrollView, then dra-n-drop works fine. But when not all controls are visible on screnn and scrolling appears, I can't use dra-n-drop. All events come to ScrollView and only scrollview proceed them. Contols didn't get touch events and as result, I can't drag my controls. Does anybody know how to resolve this issue? I suppose, that scrollview should proceed self touch event, but then it should transfer this touch event to the children controls. How can I do it? Or any other solutions?

like image 438
brave_warrior Avatar asked Sep 30 '12 10:09

brave_warrior


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.

How are Android touch events delivered?

The touch event is passed in as a MotionEvent , which contains the x,y coordinates, time, type of event, and other information. The touch event is sent to the Window's superDispatchTouchEvent() .

What is onInterceptTouchEvent?

This is basically a hit testing algorithm where you figure out which child view's bounding rectangle contains the touch point coordinates. But before it can dispatch the event to the appropriate child view, the parent can spy and/or intercept the event all together. This is what onInterceptTouchEvent is there for.

How do I use onTouchEvent on Android?

After the Math. abs() calls, you're essentially testing if their finger is farther down the screen than it is across the screen. Store the initial down coordinates as member variables and set them during ACTION_DOWN . You declared two floats (touchX and touchY) inside the onTouchEvent method.


1 Answers

Use mScrollView.requestDisallowInterceptTouchEvent(true); to avoid ScrollView to handle touchEvents. Also as Ridcully pointed out handle the touch event in onInterceptTouchEvent().

like image 131
Dayerman Avatar answered Oct 16 '22 13:10

Dayerman