Android 滚动视图内的地图不可滚动

Android 滚动视图内的地图不可滚动,android,android-layout,android-maps-v2,android-maps,Android,Android Layout,Android Maps V2,Android Maps,在垂直滚动视图中有一个地图片段。我希望它是可滚动的。但它不起作用。 我已尝试添加一个透明图像以及来自以下答案的自定义滚动视图: 我的布局结构: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView" xmlns:tools="http://schemas.a

在垂直滚动视图中有一个地图片段。我希望它是可滚动的。但它不起作用。 我已尝试添加一个透明图像以及来自以下答案的自定义滚动视图:

我的布局结构:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.TripDetailsActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ECF0F1"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorCard"
            android:orientation="vertical"
            android:weightSum="12">

            <TextView
                android:id="@+id/tripTxt"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="3"
                android:text="Trip"
                android:textColor="@color/colorTextCard"
                android:textSize="20sp" />

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_margin="10dp"
                tools:context="com.example.keelindemo.Activities.Map" />

            <TextView
                android:id="@+id/start_point"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="10dp"
                android:layout_weight="2"
                android:drawableLeft="@drawable/flag_start"
                android:drawablePadding="10dp"
                android:gravity="center_vertical"
                android:textColor="@color/colorTextCard" />

            <TextView
                android:id="@+id/end_point"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="2"
                android:drawableLeft="@drawable/flag_end"
                android:drawablePadding="10dp"
                android:gravity="center_vertical"
                android:textColor="@color/colorTextCard" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

请提出一些解决办法。我正在棉花糖上尝试一切。

你可以在scrollview中滚动谷歌地图。 请参阅下面的链接,我描述了如何在android中滚动scrollview中的地图:
您可以在scrollview中滚动谷歌地图。 请参阅下面的链接,我描述了如何在android中滚动scrollview中的地图:

您可以试试下面的代码,看看它是否适合您

View customView = null;
ScrollView scrollViewParent = null;

scrollViewParent = (ScrollView) v.findViewById(R.id.scrollViewParent);
customView = (View) v.findViewById(R.id.customView);

customView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            // Disallow ScrollView to intercept touch events.
            scrollViewParent.requestDisallowInterceptTouchEvent(true);
            // Disable touch on transparent view
            return false;

        case MotionEvent.ACTION_UP:
            // Allow ScrollView to intercept touch events.
            scrollViewParent.requestDisallowInterceptTouchEvent(false);
            return true;

        case MotionEvent.ACTION_MOVE:
            scrollViewParent.requestDisallowInterceptTouchEvent(true);
            return false;

        default:
            return true;
        }
    }
});
现在在XML文件中

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollViewParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

        <View
            android:id="@+id/customView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent" />
</ScrollView>

您可以尝试一下下面的代码,看看它是否适合您

View customView = null;
ScrollView scrollViewParent = null;

scrollViewParent = (ScrollView) v.findViewById(R.id.scrollViewParent);
customView = (View) v.findViewById(R.id.customView);

customView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            // Disallow ScrollView to intercept touch events.
            scrollViewParent.requestDisallowInterceptTouchEvent(true);
            // Disable touch on transparent view
            return false;

        case MotionEvent.ACTION_UP:
            // Allow ScrollView to intercept touch events.
            scrollViewParent.requestDisallowInterceptTouchEvent(false);
            return true;

        case MotionEvent.ACTION_MOVE:
            scrollViewParent.requestDisallowInterceptTouchEvent(true);
            return false;

        default:
            return true;
        }
    }
});
现在在XML文件中

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollViewParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

        <View
            android:id="@+id/customView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent" />
</ScrollView>
在活动/片段中,在onCreate/onViewCreated中添加以下代码

ScrollView nScrollView = (ScrollView) findViewById(R.id.scrollViewParent);
            MySupportMapFragment mSupportMapFragment;
            mSupportMapFragment = (MySupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);

            mSupportMapFragment.setListener(new MySupportMapFragment.OnTouchListener() {
                        @Override
                        public void onTouch() {
                            mScrollView.requestDisallowInterceptTouchEvent(true);
                        }
                    });

            if (mSupportMapFragment != null) {
                mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        if (googleMap != null) {
                            googleMap.getUiSettings().setAllGesturesEnabled(true);
                            googleMap.getUiSettings().setZoomControlsEnabled(true);
                            LatLng marker_latlng = new LatLng(lat, lon);
                            CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(15.0f).build();
                            CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
                            googleMap.moveCamera(cameraUpdate);
                            if (latitude != null && longitude != null) {
                                    googleMap.addMarker(new MarkerOptions().position(marker_latlng).title("Address"));
}
                        }
                    }
                });
            }
&您的布局如下所示

<fragment
        android:id="@+id/googleMap"
        class="com.yourpackagename.MySupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
在活动/片段中,在onCreate/onViewCreated中添加以下代码

ScrollView nScrollView = (ScrollView) findViewById(R.id.scrollViewParent);
            MySupportMapFragment mSupportMapFragment;
            mSupportMapFragment = (MySupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);

            mSupportMapFragment.setListener(new MySupportMapFragment.OnTouchListener() {
                        @Override
                        public void onTouch() {
                            mScrollView.requestDisallowInterceptTouchEvent(true);
                        }
                    });

            if (mSupportMapFragment != null) {
                mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        if (googleMap != null) {
                            googleMap.getUiSettings().setAllGesturesEnabled(true);
                            googleMap.getUiSettings().setZoomControlsEnabled(true);
                            LatLng marker_latlng = new LatLng(lat, lon);
                            CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(15.0f).build();
                            CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
                            googleMap.moveCamera(cameraUpdate);
                            if (latitude != null && longitude != null) {
                                    googleMap.addMarker(new MarkerOptions().position(marker_latlng).title("Address"));
}
                        }
                    }
                });
            }
&您的布局如下所示

<fragment
        android:id="@+id/googleMap"
        class="com.yourpackagename.MySupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

我试过了,它在自定义类中抛出空指针异常。public void setListenerOnTouchListener监听器{mListener=listener;}此方法显示未使用的,因此为空的mListener您可以创建接口OnTouchListener吗?您已经创建了它。。因此,是的,接口在代码I change Main活动代码中。我更改了谷歌地图的声明。请检查一下。现在它成功了,非常感谢!顺便说一句,在更新后的博客代码中更改变量名..我试过了,它在自定义类中抛出空指针异常。public void setListenerOnTouchListener监听器{mListener=listener;}此方法显示未使用的,因此为空的mListener您可以创建接口OnTouchListener吗?您已经创建了它。。因此,是的,接口在代码I change Main活动代码中。我更改了谷歌地图的声明。请检查一下。现在它成功了,非常感谢!顺便说一句,在更新后的博客代码中更改变量名称。建议您改为尝试NestedScrollView-他们添加了一些很好的增强功能,使用嵌套滚动作为名称建议。建议您改为尝试NestedScrollView-他们添加了一些很好的增强功能,使用嵌套滚动作为名称建议