Android: ScrollView拦截 map API V2 touch

标签 android scrollview google-maps-api-2 ontouchlistener

我有一个 Activity,它有一个 scrollview,可以计算很多东西......其中之一是 mapfragment api v2。最大的拖钓问题是当我垂直移动 map 时,所有的卷轴都会移动,而我不希望这样。当我触摸和移动 map 时,我真的希望 ScrollView 不要移动。

代码如下:

public class MyMap extends FragmentActivity {

...



        @Override
        public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.map);

            ...


    fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfoto);
    googleMap = fm.getMap();


    /** Here i can do something like googleMap.setOnToucListener().... 
            but it's not supported!!!! */
            ...

    }

}

和 map.xml:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/scroll"
    tools:context=".MyMap">   

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/relativeLayoutA"
            android:layout_below="@+id/mapfoto"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <fragment
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                class="com.google.android.gms.maps.SupportMapFragment" />

      ...

            </RelativeLayout>
        ...

        </RelativeLayout>


</ScrollView>    

我试过使用 OnMapClickListener() 和 OnMapLongClickListener() 但没有用

欢迎所有帮助...

最佳答案

想法是在 map fragment 区域发生滚动 Action 时,只拦截 map fragment 上的触摸事件。

您可以扩展 MapFragment 并在其之上放置一个 FrameLayout 来拦截和传达触摸操作。

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.google.android.gms.maps.SupportMapFragment;

public class OnScrollableContainerMapFragment extends SupportMapFragment {

  private OnTouchListener mOnTouchListener;

  @Override
  public View onCreateView(
      LayoutInflater layoutInflater,
      ViewGroup viewGroup,
      Bundle savedInstance
  ) {
    final View mapView = super.onCreateView(layoutInflater, viewGroup, savedInstance);

    if (mapView != null) {
      ((ViewGroup) mapView).addView(
          new TouchableWrapper(getActivity()),
          new ViewGroup.LayoutParams(
              ViewGroup.LayoutParams.MATCH_PARENT,
              ViewGroup.LayoutParams.MATCH_PARENT
          )
      );
    }

    return mapView;
  }

  public void setOnTouchListener(@NonNull final OnTouchListener onTouchListener) {
    mOnTouchListener = onTouchListener;
  }

  public interface OnTouchListener {

    void onStartScrollingMap();

    void onStopScrollingMap();
  }

  private class TouchableWrapper extends FrameLayout {

    public TouchableWrapper(@NonNull final Context context) {
      super(context);
      setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
      switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          mOnTouchListener.onStartScrollingMap();
          break;
        case MotionEvent.ACTION_UP:
          mOnTouchListener.onStopScrollingMap();
          break;
      }
      return super.dispatchTouchEvent(event);
    }
  }

}

当在 map 上滚动的 Action 开始时,您要求外部可滚动容器停止拦截触摸事件。

本地图上的滚动完成后,您告诉外部可滚动容器再次开始拦截触摸事件。

在下面的示例中,外部可滚动容器是一个 ScrollView。

请注意,示例假定代码位于 fragment 中。只是整个类文件的一部分,并且缺少其他使其编译所需的元素。

(...)
mSupportMapFragment = (OnScrollableContainerMapFragment) getChildFragmentManager()
        .findFragmentById(R.id.fragment_map);

        mSupportMapFragment
        .setOnTouchListener(new OnScrollableContainerMapFragment.OnTouchListener() {
          @Override
          public void onStartScrollingMap() {
            mScrollView.requestDisallowInterceptTouchEvent(true);
          }

          @Override
          public void onStopScrollingMap() {
            mScrollView.requestDisallowInterceptTouchEvent(false);
          }
        });
(...)

XML 布局文件如下所示。

请注意,这只是文件的一部分,其他部分缺少编译所需的必要元素。

(...)

<fragment
          (...)
            android:id="@+id/fragment_map"
            android:name="com.stackoverflow.answer.OnScrollableContainerMapFragment"
            android:tag="team_details_info_fragment_map" />
(...)

看看这里的要点:Gist

关于Android: ScrollView拦截 map API V2 touch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858214/

相关文章:

android - 将字符串变量作为参数传递以在 Android 中创建新的 Intent

Android 开发者工具 16 和混淆器中的 -keepclasseswithmembers 问题

android - 如何在 ScrollView 中滚动编辑文本

google-maps - Google Maps API 3 fitBounds 填充 - 确保标记不会被覆盖的控件遮挡

php - 将多个图像连同 android 中的标签一起发送到服务器

android - 如何访问 SearchView 子项?

android - 决策 : ListView or ScrollView

android - 在 android 中的 scrollView 之后没有小部件显示

iphone - 如何为iPhone提供 map 导航支持?

android - 找不到从方法 glt.a 引用的类 'com.google.android.gms.location.internal.ParcelableGeofence