Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView is catching touch event for google map

I have a horizontal scroll view that contains a hierarchy of viewgroups and then finally a google map. My problem is that the HSV is catching the left-right drag that's meant for the map. I've tried

    hsv.requestDisallowInterceptTouchEvent(true);

and even

    mapView.getParent().requestDisallowInterceptTouchEvent(true);

but to no avail. Is there anything I'm doing wrong here? Or can you suggest another solution?

I think this should have been my original question: How do I implement the solution posted here Mapview inside a ScrollView. Specifically, where do I put the code?

like image 650
user1923613 Avatar asked Dec 24 '12 21:12

user1923613


3 Answers

It seems you on the right way, but you should call requestDisallowInterceptTouchEvent(true) method on every touch event (see docs). Try this solution

Updated:

Try this out:

final HorizontalScrollView hsv = ...
final MapView mapView = ...

mapView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                hsv.requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                hsv.requestDisallowInterceptTouchEvent(false);
                break;
        }
        return mapView.onTouchEvent(event);
    }
});
like image 150
Alex Vasilkov Avatar answered Nov 19 '22 16:11

Alex Vasilkov


For google map v2, follow the solution is this tutorial

like image 32
Lorensius W. L. T Avatar answered Nov 19 '22 16:11

Lorensius W. L. T


You have to create custom MapView. Follow the code snippet provided below

public class AppMapView extends MapView {

    public AppMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
               System.out.println("unlocked");
               this.getParent().requestDisallowInterceptTouchEvent(false);
               break;

            case MotionEvent.ACTION_DOWN:
               System.out.println("locked");
               this.getParent().requestDisallowInterceptTouchEvent(true);
               break;
       }
       return super.dispatchTouchEvent(ev);
   }
}

In XML follow code below:

<com.tech.linez.brixlanepassenger.custom_views.AppMapView
   android:id="@+id/map_ride_route"
   android:layout_width="match_parent"
   android:layout_height="220dp"
   android:layout_margin="10dp"/>
like image 1
Hantash Nadeem Avatar answered Nov 19 '22 17:11

Hantash Nadeem