使用嵌套的MapView时禁止拦截ListView的触摸事件

5

我希望在移动、缩放和捏合地图视图时,禁止拦截Listview的触摸操作,并且地图视图位于Listview的头部视图中。

我有一个包含所有商店的ListView。我已经将另一个布局xml设置为List View Header并将其添加到主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 是一个扩展了 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);
}

我已经编写了代码来禁止拦截父视图的触摸事件。

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);
        }
    });

很遗憾,它没有起作用。

但是当将OnTouchListener设置为ListView对象时,它会记录事件。

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;
        }
    });

那么,我该如何从这种情况中摆脱?
是否有可能我们无法获取 'OnTouchListerner' 以便对 ListView 的子项 -> 页眉视图的子项 -> MapView 进行操作?

任何帮助都将不胜感激。


你有解决方案吗,@Imanan? - Rethinavel
1个回答

5

重写dispatchTouchEvent()方法,阻止ListView在ACTION_DOWN事件上拦截触摸事件。在我的情况下,我将其实现在包含MapView的ViewGroup中,但如果你扩展MapView,它也可能很好地工作。唯一需要的是对MapView所在的ListView的引用。

@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);
}

这可能适用于大多数/所有具有滚动机制的容器。

1
谢谢您!我可以确认这也适用于MapView子类。只需使用getParent()替换listView即可。 - Mike Cole

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接