【问题标题】:Set RecyclerView Scroll first and then viewpager swipe in CoordinatorLayout在 CoordinatorLayout 中设置 RecyclerView 先滚动然后 viewpager 滑动
【发布时间】:2026-02-17 12:00:02
【问题描述】:

我对典型的 CoordinatorLayout + AppBarLayout + Toolbar + TabLayout + Viewpager 有一个非常烦人的问题,其中工具栏的 layout_scrollFlags 带有“scroll|enterAlways|snap”

这里是一个带有 NavigationView 的 DrawerLayout 的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_tabs_main_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/activity_tabs_main_coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/activity_tabs_main_appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/material_toolbar"
                style="@style/AppCompatTheme.Toolbar"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                app:popupTheme="@style/material_toolbar_popup"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/activity_tabs_main_tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabIndicatorColor="@color/neon_green"/>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/activity_tabs_main_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_item"/>

</android.support.v4.widget.DrawerLayout>

我注意到的真正的两个问题是,如果我尝试对 RecyclerView 进行对角滑动,我可以在 Google Play 应用程序或 Whatsapp 或 Chris Bane 的 Cheesesquare 中以对角方式滑动 viewpager 中的选项卡(见附图)先做手势,然后向下/向上滚动。

我认为这个 ViewPager 的行为使工具栏在设置导航器抽屉图标的地方创建了一个小反弹,在工具栏标题旁边。

所以真正的问题是试图防止这个反弹问题或错误,你也可以查看这个视频

Youtube 链接:Video to show the bounce effect in the Toolbar

我已经试过去掉 DrawerLayout 和 NavigationView 但没有效果,弹跳问题还在继续。

提前致谢!

【问题讨论】:

  • 如果您尝试将 ViewPager 包装在 NestedScrollView 中,并在 NestedScrollView 中包含 app:layout_behavior
  • 因为 Chris Bane 在 cheesesquare 中没有这样做,但我会尝试。如果我将 viewpager 包装在 FrameLayout 中会怎样?
  • +什么都没做的WindRider =(

标签: android-viewpager android-coordinatorlayout android-appbarlayout


【解决方案1】:

我想我找到了解决这个问题的方法。在RecyclerView.ViewHolder 中添加View.OnClickListener() 后,几乎无法察觉,这可能吗?

这是RecyclerView:里面的代码

    private RecyclerOnItemClickListener itemClickListener;

    class PostDtoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final PostView postView;

        public PostDtoViewHolder(PostView postView) {
            super(postView);
            this.postView = postView;
            this.postView.setOnClickListener(this);
        }

        public View getViewHolder() {
            return this.postView;
        }

        public void bind(PostDto postDto) {
            postView.loadContent(postDto);
        }

        @Override
        public void onClick(View v) {
            itemClickListener.onItemClickListener(getAdapterPosition());
        }
    }

其中RecyclerOnItemClickListener只是方法的接口:

public interface RecyclerOnItemClickListener {

    void onItemClickListener(int itemPosition);
}

【讨论】: