【发布时间】:2015-12-01 00:30:01
【问题描述】:
我使用 Android 设计库 制作了一个应用程序,带有一个工具栏和 TabLayout。
实际上存在 2 个选项卡,都带有 2 个 RecyclerView,滚动时会自动折叠工具栏。
我的问题是:当 RecyclerView 的项目很少且完全适合屏幕时(如 TAB 2 中),我可以禁用工具栏折叠吗?
我见过很多类似CheeseSquare 的例子,由 Google 员工制作,但问题仍然存在:即使 RecyclerView 只有 1 个项目,工具栏仍会在滚动时隐藏。
我想我可以找出 RecyclerView 的第一项是否在屏幕上可见,如果是,则禁用工具栏折叠。前者容易实现,后者呢?
这是我的布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
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:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/glucosio_pink"
app:tabSelectedTextColor="@android:color/white"
app:tabIndicatorColor="@color/glucosio_accent"
app:tabTextColor="#80ffffff"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_fab"
android:layout_margin="16dp"
android:onClick="onFabClicked"
app:backgroundTint="@color/glucosio_accent"
android:src="@drawable/ic_add_black_24dp"
android:layout_gravity="bottom|right"
/>
</android.support.design.widget.CoordinatorLayout>
【问题讨论】:
-
你能告诉我你是如何制作这个 gif 的吗?
-
嗨@PratikButani!我在互联网上找到了 gif (mzgreen.github.io/2015/06/23/…)。顺便说一句,您可以使用 gifs.com 之类的服务从 YouTube 视频中创建 gif。
-
用你的链接发给那个人 :) 愿他能帮助你。谢谢 :) twitter.com/pratik13butani/status/644355243174526976
-
期望它有什么作用?假设您在第一页折叠工具栏,然后滑动到第二页。应该发生什么?工具栏已隐藏,您无法显示它,因为项目太少。并且为了禁用工具栏滚动 - 有可能,我稍后会玩它。也许自定义行为将成为解决方案。
-
请将您的解决方案写成正确的答案,以便于查找。
标签: android toolbar android-recyclerview android-appcompat android-design-library