简体   繁体   English

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

[英]Collapsing Toolbar with ViewPager2 gives scrolling issues

So I have this weird problem that only happens with the ViewPager2 that I've been trying to solve but have never seen anything like this reported before.所以我遇到了这个奇怪的问题,它只发生在我一直试图解决的 ViewPager2 上,但以前从未见过这样的报告。

I'm trying to coordinate a ViewPager2 with a ToolBar to make it hide whenever the user scrolls the content inside it.我正在尝试将 ViewPager2 与 ToolBar 进行协调,以使其在用户滚动其中的内容时隐藏。 It works fine when loading a fragment with a populated RecyclerView but behaves weird when the recycler is empty or there is no fragment at all.当加载带有填充 RecyclerView 的片段时它工作正常,但当回收器为空或根本没有片段时表现得很奇怪。

Here are some gifs to shows the problem(First ViewPager2(Bug), Second ViewPager(Correct)).这里有一些 gif 来显示问题(第一个 ViewPager2(错误),第二个 ViewPager(正确))。

As you can see in the first image it works correctly only if I select the toolbar but gives dragging problems whenever I grab the empty space where the NestedScrollView is.正如您在第一张图片中看到的,只有当我选择工具栏时它才能正常工作,但是每当我抓住 NestedScrollView 所在的空白空间时都会出现拖动问题。

ViewPager2 的 Gif ViewPager 的 Gif

And here is the xml layout for the ViewPager2:这是 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>

After a lot of time I finally solved it by removing the NestedScrollView and replacing it with the actual ViewPager2.经过很多时间,我终于通过删除 NestedScrollView 并将其替换为实际的 ViewPager2 来解决它。 Just remember to always setup a fragment or otherwise the toolbar will not scroll.请记住始终设置一个片段,否则工具栏将不会滚动。

Here is the ViewPager2:这是 ViewPager2:

<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" />

EDIT: If you still need to use a NestedScrollView put it inside the fragment and it will behave as expected.编辑:如果您仍然需要使用 NestedScrollView 将它放在片段中,它将按预期运行。

Use ViewPager instead of ViewPager2.使用 ViewPager 而不是 ViewPager2。 Because custom viewpager create issue in every scroll outside it.因为自定义 viewpager 在它外面的每个滚动中都会产生问题。 Like collapsing toolbar, switching tabs etc.像折叠工具栏,切换标签等。

Unable to merely add comment but wanted to reiterate that correct solution is to fall back to use of ViewPager rather than ViewPager2.无法仅添加评论但想重申正确的解决方案是回退到使用 ViewPager 而不是 ViewPager2。

I have a single activity with NavigationDrawer andn expanding action bar.我有一个带有 NavigationDrawer 和扩展操作栏的活动。 UI starts with showing a MasterFragment whichi provides self-referencing drill down. UI 从显示一个 MasterFragment 开始,它提供了自引用向下钻取。 At some point when you click on an item in master with actual content I show another fragment.在某些时候,当您单击具有实际内容的 master 中的项目时,我会显示另一个片段。 Clicking on this fragment shows a 3-tab view and two of the tabs are WebView.单击此片段会显示一个 3 选项卡视图,其中两个选项卡是 WebView。 Vertical scrolls in WebView were horribly broken and always inadvertently switched you to another tab. WebView 中的垂直滚动严重损坏,并且总是不经意间将您切换到另一个选项卡。 Switching back to ViewPager totally fixed these issues.切换回 ViewPager 完全解决了这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM