Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disallow intercept touches of Listview while using its Child MapView

I want to disallow intercept touches of Listview while Moving, zooming & pinch in MapView which is inside Listview's Header View.

I have one ListView contains all stores. I have set one List View Header as another layout xml and adding it to Main ListView.

ListView Header

<com.google.android.gms.maps.MapView
    android:id="@+id/mapViewStores"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="10dp" />

StoreList.java which extends Fragment

MapView mapView;        //Map View
GoogleMap mapStoreList; //For Markers on Map

listViewStoreData.addHeaderView(headerViewForStoreList);

mapView = (MapView) headerViewForStoreList
                .findViewById(R.id.mapViewStores);
        mapView.onCreate(savedInstanceState);

if (mapView != null) {
    mapStoreList = mapView.getMap();
    mapStoreList.getUiSettings().setMyLocationButtonEnabled(true);
    mapStoreList.setMyLocationEnabled(true);
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                    new LatLng(latitude, longitude), 13);
    mapStoreList.animateCamera(cameraUpdate);
}

I have set code for disallow for Intercept touches of parent view

mapStoreList.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_UP:
                this.getParent().requestDisallowInterceptTouchEvent(false);
                break;

            case MotionEvent.ACTION_DOWN:
                this.getParent().requestDisallowInterceptTouchEvent(true);
                break
            }

            return super.onTouchEvent(ev);
        }
    });

Unfortunately, it's not working.

But when set OnTouchListener to ListView object, it'll log events.

listViewStoreData.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                Log.e("MotionEvent", "Move");
                break;

            case MotionEvent.ACTION_UP:
                Log.e("MotionEvent", "Up");
                break;

            case MotionEvent.ACTION_DOWN:
                Log.e("MotionEvent", "Down");
                break;

            case MotionEvent.ACTION_CANCEL:
                Log.e("MotionEvent", "Cancel");
                break;

            case MotionEvent.ACTION_POINTER_INDEX_MASK:
                Log.e("MotionEvent", "Pointer Index Mask");
                break;

            case MotionEvent.ACTION_POINTER_INDEX_SHIFT:
                Log.e("MotionEvent", "Pointer Index Shift");
                break;
            }

            return false;
        }
    });

So, how do I overcome from this situation ?
Is there any possibility that we cannot get 'OnTouchListerner' for ListView's Child -> Header View's Child -> MapView ??

Any help will be appreciated.

like image 535
Manann Sseth Avatar asked Oct 02 '22 02:10

Manann Sseth


1 Answers

Override dispatchTouchEvent() and stop the listview from intercepting touch events on ACTION_DOWN. In my case, I implemented it in the ViewGroup containing my MapView, but it'd probably work fine if you extended MapView instead as well. The only thing you need is a reference to the ListView the MapView resides within.

@Override
public boolean dispatchTouchEvent(final MotionEvent motionEvent) {

    switch (motionEvent.getActionMasked()) {
        // Pressed on map: stop listview from scrolling
        case MotionEvent.ACTION_DOWN:
            listView.requestDisallowInterceptTouchEvent(true);
            break;

        // Released on map or cancelled: listview can be normal again
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            listView.requestDisallowInterceptTouchEvent(false);
            break;
    }

    // Process event as normal. If listview was disallowed touch events, the map will process the event.
    return super.dispatchTouchEvent(motionEvent);
}

This would likely work with most/all containers that have a scrolling mechanism.

like image 158
Tom Avatar answered Oct 03 '22 16:10

Tom