Android CoordinatorLayout与ViewPager结合时的奇怪行为
我的应用程序中有以下主页布局: ViewPager适配器中的所有片段内部都有一个RecyclerView。 当我第一次向上滚动RecyclerView的内容(以便隐藏应用程序栏),然后切换到ViewPager中的另一页数据并向下滚动RecyclerView的内容时,应用程序栏是。。。看不见的 导致此问题的主要步骤:Android CoordinatorLayout与ViewPager结合时的奇怪行为,android,android-viewpager,android-coordinatorlayout,android-appbarlayout,Android,Android Viewpager,Android Coordinatorlayout,Android Appbarlayout,我的应用程序中有以下主页布局: ViewPager适配器中的所有片段内部都有一个RecyclerView。 当我第一次向上滚动RecyclerView的内容(以便隐藏应用程序栏),然后切换到ViewPager中的另一页数据并向下滚动RecyclerView的内容时,应用程序栏是。。。看不见的 导致此问题的主要步骤: 在api级别为11或更高的设备上运行(在api级别低于11的设备上,一切正常) 向上滚动内容 切换到另一个ViewPager的页面 向下滚动内容,使应用程序栏可见 应用程序栏不可见
Toolbar toolbar = (Toolbar) view.findViewById(R.id.actionToolbar);
toolbar.setTitle(toolbarTitleResId);
toolbar.inflateMenu(menuResId);
toolbar.setOnMenuItemClickListener(listener);
EDIT2:片段的布局
我能够在我的API 16设备上重现这一点(API 22上仍然没有问题)。这肯定是AppBarLayout中的一个bug,但是有一个快速解决方法。只有当AppBarLayout完全隐藏时,这才是一个问题。在AppBarLayout中添加一个高度为1dp的视图,所有内容都可以正常工作,而不会在视图上引起注意
<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>
...
我们也可以将此解决方案应用于切换到另一个ViewPager的页面
appBarLayout.setExpanded(true, true);
这里是第二个参数,如果它是真的,AppBarLayout将通过动画展开。我在您发布的代码中没有看到任何突出的内容。你能在片段和片段布局文件中添加工具栏初始化代码吗?有了添加的片段代码,我的API 22设备上的一切仍然正常工作。我建议为工具栏提供不同的id以避免命名冲突,但即使使用相同的id,我也无法重现该问题。如果您可以在此处发布更多代码或将项目放在Github上,我很乐意再看一看。@blackcj,好的,我将尝试使我的代码更干净,如果我没有找到错误,我将把它放在Github上。据我所知,协调布局的子项必须是AppBarLayout、NestedScrollView,FloadingActionButton或从这些布局之一继承的任何子布局。将viewpager添加为CoordinatorLayout的直接子级可能会使该子级不可见或无法正确渲染。我尝试过其他布局组,但没有使用ViewPager。我没有愉快的经历。它非常凌乱,被迫改为标准布局。这个解决方案工作得非常完美!谢谢你的回复。
<?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>
<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>
appBarLayout.setExpanded(true, true);