【问题标题】:Hide floating button when collapsing toolbar is collapsed折叠工具栏折叠时隐藏浮动按钮
【发布时间】:2016-04-29 12:38:51
【问题描述】:

我有一个协调器布局,其中包含一个折叠工具栏、一个嵌套滚动视图和一个浮动按钮。 我的浮动按钮位于折叠工具栏的右下端。 我希望在折叠工具栏完全折叠时按钮消失。并在其他情况下可见。

我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="fr.djey.secretapp.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:id="@+id/appBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <ImageView
                android:id="@+id/collapsing_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                android:scaleType="centerCrop"
                android:src="@drawable/welcome"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"/>

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/content_frame">

        </FrameLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-60dp"
        android:layout_marginRight="16dp"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/collapse_toolbar"
        app:layout_anchorGravity="bottom|right|end" />

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

有可能吗?如何实现?

【问题讨论】:

    标签: android android-design-library android-collapsingtoolbarlayout


    【解决方案1】:

    为工具栏设置偏移侦听器将为您提供更大的灵活性:您可以决定在工具栏折叠/打开时会发生什么。

        appBarLayout.addOnOffsetChangedListener(
        AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
        if (verticalOffset == 0) {
            showFAB()
            // logic when toolbar is open (for example show fab)
         } else {
            hideFAB()
            // logic when toolbar has collapsed (for example hide fab)
         })
    

    如果您想知道工具栏何时位于 / 之间,您需要 java 代码: https://stackoverflow.com/a/39424318/11878751

    【讨论】:

      【解决方案2】:

      试试这个小改变

      <android.support.design.widget.FloatingActionButton
          ...
          app:layout_anchor="@id/appBar"
          app:layout_anchorGravity="bottom|right|end" />
      

      【讨论】:

      • 谢谢,太好了!但是你能告诉我如何在 java 中检查这个折叠状态,以便在它折叠时更改锚点状态吗?
      • 你尝试过Android Collapsingtoolbarlayout Collapse Listener这里的解决方案吗?
      • 谢谢,我试着把它放在 java onCreate 中:appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() 但我在这一行得到了一个空指针异常
      • 也许你忘了用findViewById() 或你在onOffsetChanged() 方法中引用的另一个视图来初始化appBar 视图
      • 如果 FloatingActionButton 位于一个布局 (xml) 文件中,如果是片段,而 AppBar 位于不同布局的文件中呢?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多