【发布时间】:2015-10-07 08:12:23
【问题描述】:
我正在尝试实现 Toolbar 和 TabLayout,当您滚动回收视图时,它应该像 whatsapp 应用程序一样隐藏工具栏。我指的是谷歌Blog 和Michal Z's Blog。我在主要活动中有查看寻呼机,我正在使用 recycleview 显示其中的数据列表。
坦率地说,我被新的谷歌设计库所震撼,因为只需一行 XML 代码就可以改变完整的 UI 和 UI 行为。所以理想情况下,我们需要在工具栏中添加app:layout_scrollFlags="scroll|enterAlways" 行,它就可以工作了。但对我来说它不起作用。我已经在 API 19 和 21 设备上对其进行了测试。
通过查看堆栈溢出时的question question 和各种Blog Blog,我找到了实现此功能的两种方法:1. 通过为recycleview 提供滚动侦听器和2. 将可滚动内容放入NestedScrollView .但它没有按预期工作。
我认为它应该可以在不编写任何代码的情况下工作。
主Activity.xml
<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.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Dark"/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
ViewPagerFragment.xml
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/windowBackground">
<android.support.v7.widget.RecyclerView
android:id="@+id/order_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft= "true"
android:layout_alignParentStart="true"
android:layout_above="@+id/txtStatus" />
</android.support.v4.widget.SwipeRefreshLayout>
【问题讨论】:
-
请发布您的
build.gradle文件。 -
我遇到了同样的问题,appbarlayout 在 api 9 平板电脑上没有滚动
标签: android android-toolbar android-design-library android-coordinatorlayout android-appbarlayout