android - CoordinatorLayout 与 ViewPager 的奇怪行为

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

我的应用程序中有以下主页面布局:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/actionToolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:title="@string/app_name"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways">
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways">
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

可滚动的内容是 ViewPager。我将 ViewPager 与 TabLayout 结合使用:

ViewPager viewPager = (ViewPager) v.findViewById(R.id.mainPager);
TabLayout tabLayout = (TabLayout) v.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

ViewPager 的适配器中的所有 fragment 都在内部有一个 RecyclerView。 当我第一次向上滚动 RecyclerView 的内容(以便隐藏应用程序栏),然后切换到 ViewPager 中的另一页数据并向下滚动 RecyclerView 的内容时,应用程序栏是......不可见的。 导致这个问题的主要步骤:

  1. 在 api 级别 11 或更高级别的设备上运行(在 api 级别低于 11 的设备上一切正常);
  2. 向上滚动内容;
  3. 切换到另一个 ViewPager 的页面;
  4. 向下滚动内容以使应用栏可见;
  5. 应用栏是不可见的。

我的问题在哪里?谢谢。

EDIT1:Fragment 的工具栏初始化

Toolbar toolbar = (Toolbar) view.findViewById(R.id.actionToolbar);
toolbar.setTitle(toolbarTitleResId);
toolbar.inflateMenu(menuResId);
toolbar.setOnMenuItemClickListener(listener);

EDIT2: fragment 的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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/actionToolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:background="?attr/colorPrimary">
    </android.support.v7.widget.Toolbar>

    <View
        android:id="@+id/anchor"
        android:layout_height="8dp"
        android:layout_width="match_parent"
        android:background="@drawable/shadow_gradient_drawable"
        android:layout_below="@+id/actionToolbar">
    </View>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/verticalRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/anchor"/>
</RelativeLayout>

最佳答案

我能够在我的 API 16 设备上重现这个(在 API 22 上仍然没有问题)。这绝对是 AppBarLayout 中的一个错误,但是,有一个快速的解决方法。这只是当 AppBarLayout 完全隐藏时的一个问题。向 AppBarLayout 添加一个高度为 1dp 的 View ,一切都应该正常工作,而不会在 View 上真正引起注意。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <!-- Toolbar and tab bar code -->
    ...

    <!-- Add this -->
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimary" />
</android.support.design.widget.AppBarLayout>

关于android - CoordinatorLayout 与 ViewPager 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165700/

相关文章:

android - 父 View 和 subview 的触摸监听器

java - 如何将谷歌地图/代码放入viewpager中

android - CoordinatorLayout 末尾的 float 操作按钮

android - 根据Appbar的偏移量或高度调整 View 大小

java - Android TextView 中的多个可点击字符串

android - 启动 Android 程序时出错

android - ViewPager 内部 fragment 问题

android - 是否可以在 Android Q 上添加网络配置?

android - Firebase Analytics 调试 View 不显示任何内容

android - 来自 CoordinatorLayout.Behavior 的 onDependentViewChanged 一直被调用