嵌套滚动视图中的 StreetViewPanoramaView 不起作用

StreetViewPanoramaView inside Nested scroll view not working

我在滚动视图内的 activity 中有两个片段。我可以同时显示两者,但是当我想捏合缩放或移动街景时,它无法正常工作。我有任何解决方案可以让它工作。

这是 activity 视图:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    // fragment 1 related view
    <ImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    // fragment 2 related view
    <com.google.android.gms.maps.StreetViewPanoramaView
        android:id="@+id/streetViewPanorama"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/streetView" />

</androidx.core.widget.NestedScrollView>

我采用了 CustomStreetViewPanoramaView class 扩展 StreetViewPanoramaView class 然后将 requestDisallowInterceptTouchEvent 扩展为 true,将 requestDisallowInterceptTouchEvent 扩展为 false。

class CustomStreetViewPanoramaView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : StreetViewPanoramaView(context, attrs, defStyleAttr) {

    override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                parent.parent.requestDisallowInterceptTouchEvent(true)
            }
            MotionEvent.ACTION_UP -> {
                parent.parent.requestDisallowInterceptTouchEvent(false)
            }
        }
        return super.onInterceptTouchEvent(event)
    }

}

然后在 XML 而不是使用 com.google.android.gms.maps.StreetViewPanoramaView 将其替换为您自定义的 class 和包 (com.yourpackage.name.CustomStreetViewPanoramaView),例如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    // fragment 1 related view
    <ImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    // fragment 2 related view
    <com.yourpackage.name.CustomStreetViewPanoramaView
        android:id="@+id/streetViewPanorama"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/streetView" />

</androidx.core.widget.NestedScrollView>

现在一切正常。可能对某人有用。