Java 使用ViewPager2折叠工具栏会出现滚动问题

Java 使用ViewPager2折叠工具栏会出现滚动问题,java,android,android-toolbar,android-nestedscrollview,Java,Android,Android Toolbar,Android Nestedscrollview,所以我有一个奇怪的问题,只发生在ViewPager2上,我一直试图解决这个问题,但以前从未见过类似的报道 我正在尝试将ViewPager2与工具栏协调,以便在用户滚动其中的内容时将其隐藏。 当加载带有填充的RecyclerView的片段时,它可以正常工作,但当recycler为空或根本没有片段时,它的行为会很奇怪 下面是一些GIF来说明问题(第一个ViewPager2(错误),第二个ViewPager(正确)) 正如您在第一幅图像中看到的,只有在我选择 但每当我抓取空工具栏时,工具栏就会出现拖动

所以我有一个奇怪的问题,只发生在ViewPager2上,我一直试图解决这个问题,但以前从未见过类似的报道

我正在尝试将ViewPager2与工具栏协调,以便在用户滚动其中的内容时将其隐藏。 当加载带有填充的RecyclerView的片段时,它可以正常工作,但当recycler为空或根本没有片段时,它的行为会很奇怪

下面是一些GIF来说明问题(第一个ViewPager2(错误),第二个ViewPager(正确))

正如您在第一幅图像中看到的,只有在我选择 但每当我抓取空工具栏时,工具栏就会出现拖动问题 NestedScrollView所在的空间

下面是ViewPager2的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolBar"
        style="@style/Widget.MaterialComponents.Toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"/>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.TabLayout.Colored" >

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test" />

    </com.google.android.material.tabs.TabLayout>

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


使用ViewPager而不是ViewPager2。因为自定义viewpager在其外部的每个滚动中都会产生问题。例如折叠工具栏、切换选项卡等。

经过一段时间后,我终于解决了这个问题,删除了嵌套的ScrollView,并将其替换为实际的ViewPager2。只需记住始终设置片段,否则工具栏将不会滚动

以下是ViewPager2:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolBar"
        style="@style/Widget.MaterialComponents.Toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"/>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.TabLayout.Colored" >

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test" />

    </com.google.android.material.tabs.TabLayout>

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


编辑:如果您仍然需要使用NestedScrollView,请将其放入片段中,它将按预期运行。

无法仅添加注释,但希望重申正确的解决方案是使用ViewPager而不是ViewPager2


我使用NavigationDrawer和扩展操作栏进行了一项活动。UI首先显示一个主片段,它提供了自引用向下钻取。在某个点上,当您在master中单击具有实际内容的项目时,我会显示另一个片段。单击此片段显示一个3选项卡视图,其中两个选项卡为WebView。WebView中的垂直卷轴严重损坏,总是不经意地将您切换到另一个选项卡。切换回ViewPager完全解决了这些问题。

我无法使用普通的ViewPager,因为添加/删除选项卡时出现问题我尝试在我的应用程序中实现ViewPager 2,但由于性能问题,最终返回到ViewPager。我认为ViewPager2还没有完全优化。也许使用原始的ViewPager解决这些问题并坚持使用它会更好。您能在回答中提供任何限制、假设或简化吗。有关如何回答此链接的更多详细信息:请澄清“设置片段”是什么意思?@XZen在(片段)中添加一些内容,否则,卷轴无法工作。