android – 与ViewPager一起使用CoordinatorLayout的奇怪行为

我的应用程序中有以下主页面布局:
<?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适配器中的所有片段都有一个RecyclerView.
当我第一次向上滚动RecyclerView的内容(以便隐藏应用栏)时,然后切换到ViewPager中的另一页数据并向下滚动RecyclerView的内容,应用栏是……隐身的.
导致此问题的主要步骤:

>在api级别为11或更高级别的设备上运行(在api级别低于11的设备上运行都可以);
>滚动内容;
>切换到另一个ViewPager的页面;
>向下滚动内容以使应用栏可见;
> app栏是隐形的.

我的问题在哪里?谢谢.

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完全隐藏时,这只是一个问题.添加一个高度为1dp的视图到AppBarLayout,一切都应该工作,而不会在视图上显而易见.
<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 – 与ViewPager一起使用CoordinatorLayout的奇怪行为全部内容,希望文章能够帮你解决android – 与ViewPager一起使用CoordinatorLayout的奇怪行为所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。