【问题标题】:Android Design Support Library: Use ViewPager/TabLayout with scrolling toolbar and sticky buttonAndroid 设计支持库:使用带有滚动工具栏和粘性按钮的 ViewPager/TabLayout
【发布时间】:2015-07-28 10:01:20
【问题描述】:

我有一个 TabLayout 与一个 ViewPager 相结合。在选项卡中会显示一些片段。一个 Fragment 包含一个 NestedScrollView 和一个粘性按钮,即使用户向下/向上滚动,它也应该始终可见。 此外,如果用户向下滚动,我想隐藏工具栏。

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

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

包含 ViewPager 和 Toolbar 的布局:

<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 和粘滞按钮的片段:

<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>

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

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

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

你有什么想法吗?

【问题讨论】:

  • 您不认为Snackbar 是您问题的答案吗?
  • 或者你想一直显示这个按钮?我想你可以考虑重新设计一下。
  • 此按钮应始终可见。无需重新设计。请注意,这些屏幕截图来自 Chris Banes 采用的芝士蛋糕应用程序。这不是我当前应用程序的设计。但我表现出同样的不当行为。

标签: android android-design-library


【解决方案1】:

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

android:layout_gravity="bottom|center_horizontal"

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

【讨论】:

    【解决方案2】:

    这样写NestedScrollView

    <android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginBottom="52dp"
     app:layout_behavior="@string/appbar_scrolling_view_behavior">
    

    并像这样设置Button

    <Button
     android:layout_width="match_parent"
     android:layout_height="52dp"
     android:elevation="10dp"
     android:layout_marginTop="-55dp"
     android:layout_gravity="bottom"
     android:text="Button" />
    

    如果可行,请告诉我

    【讨论】:

    • 我这样做了,但是在将 marginBottom 添加到 NestedScrollView 时滚动行为停止工作
    【解决方案3】:

    在您的片段中,将父级作为相对布局放置按钮放入 Framelayout 并将 android:layout_alignParentBottom="true" this 放入 Framelayout。

         <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">
    <RelativeLayout
        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>
        <FrameLayout android:id="@+id/test"   
       android:layout_width="match_parent"
       android:layout_alignParentBottom="true"
       android:layout_height="match_parent">
        <Button
          android:layout_width="match_parent"
          android:layout_height="52dp"
          android:text="Button" />
    </FrameLayout>
    </RelativeLayout>
        </FrameLayout>
    

    试试这个代码我修改它。

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 2015-08-23
      • 2015-09-23
      • 2017-11-24
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多