Android 设计支持库 : Use ViewPager/TabLayout with scrolling toolbar and sticky button

标签 android android-design-library

我有一个 TabLayout 和一个 ViewPager。在选项卡中显示了一些 fragment 。一个 fragment 由一个 NestedScrollView 和一个应该始终可见的粘性按钮组成,即使用户向下/向上滚动也是如此。 另外,如果用户向下滚动,我想隐藏工具栏。

目前滚动有效,但我的粘性按钮在启动时不可见,只有在我向下滚动时才会出现。

这是我的设置(基本上取自 Chris Banes cheese-sample https://github.com/chrisbanes/cheesesquare ):

包含 ViewPager 和工具栏的布局:

<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    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:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        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.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_done" />

</android.support.design.widget.CoordinatorLayout>

我的带有 NestedScrollView 和 Sticky Button 的 fragment :

<FrameLayout android:id="@+id/container"
   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.widget.NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginBottom="52dp"
  app:layout_behavior="@string/appbar_scrolling_view_behavior">

  <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

     <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="Item 1" />

     <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="Item 2" />

     <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="Item 3" />

     <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="Item 4" />

     <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="Item 5" />

  </LinearLayout>
</android.support.v4.widget.NestedScrollView>

<Button
  android:layout_width="match_parent"
  android:layout_height="52dp"
  android:layout_gravity="bottom"
  android:text="Button" />
</FrameLayout>

我想要达到的结果(启动后的初始状态)- 应该显示带有文本“Button”的按钮 - 请忽略 FAB:

Result to achieve

这是我目前得到的结果 - 带有文本“Button”的按钮不可见 - 请忽略 FAB:

current result

如果我向下滚动按钮“按钮”应该保持可见并且工具栏应该折叠:

after scrolling

你有什么想法吗?

最佳答案

您需要将粘性按钮放入带有属性的根 CoordinatorLayout

android:layout_gravity="bottom|center_horizontal"

或者使粘性按钮的底部边距等于 ?android:actionBarSize

关于Android 设计支持库 : Use ViewPager/TabLayout with scrolling toolbar and sticky button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673078/

相关文章:

android - 如何在 ViewPager fragment 中应用共享元素动画?

android - 小写的 TabLayout 选项卡标题文本

java - OpenCV java.lang.UnsatisfiedLinkError

android - NavigationView 打开速度慢/跳帧(Android 设计库)

Android NestedScrollView fillViewport 拉伸(stretch)不正确

android - 剪辑或删除折线谷歌地图 v2 的一部分

android - 工具栏不显示滑动刷新

android - Facebook URL 方案不起作用 fb ://publish

java - 如何将事件与微调器选择相关联

java - 单击时如何更改图像按钮背景?