android - 禁止在使用其子 MapView 时拦截 Listview 的触摸

标签 android android-listview android-mapview ontouchlistener intercept

我想在 Listview 的标题 View 内的 MapView 中移动、缩放和捏合时禁止拦截 Listview 的触摸

我有一个包含所有商店的ListView。我已将一个 List View Header 设置为另一个布局 xml 并将其添加到 Main ListView。

ListView 标题

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

我已经设置了禁止拦截父 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);
        }
    });

不幸的是,它不起作用。

但是当将 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;
        }
    });

那么,我该如何克服这种情况呢?
是否有可能我们无法为 ListView 的 Child -> Header 获取“OnTouchListerner” View 的 subview -> MapView ??

任何帮助将不胜感激。

最佳答案

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

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

关于android - 禁止在使用其子 MapView 时拦截 Listview 的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22905130/

相关文章:

android - 更改安卓 :digits programmatically

android - Fragment 中的 ListView 不显示

android - MapView.onMapReady 从未在 Fragment 中调用以在 MapView 中加载 Google map

java - 如何在android中的插槽类型中获取时间

android - 如何在 Youtube Android Player API 中启用纸板模式?

android - 将 meteor 应用程序作为 android 应用程序运行

java - 限制listview,只显示字母

Android 自动合并两个 ListView 在一个屏幕上

Android:MapActivity 给出 Nullpointerexception

android mapview 标记大小