【问题标题】:Floating Buttons and Bottom Sheet Fragment浮动按钮和底页片段
【发布时间】:2018-01-31 13:27:27
【问题描述】:

我正在努力使用一个浮动按钮,该按钮根据底部工作表片段的位置上下移动。 我想要像谷歌地图一样的东西,无论底部工作表是否移动,浮动按钮也会一起移动。

此时我的活动是这样的

我有一个在片段中加载地图的活动,像这样

<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"
    tools:context="br.com.vix.v1motorista.MotoristaActivity">

<!-- here the map is loaded -->
<FrameLayout
    android:id="@+id/conteudo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:layout_marginBottom="16dp"
    android:visibility="visible"
    app:layout_anchor="@+id/conteudo"
    app:layout_anchorGravity="right|bottom"
    app:srcCompat="@drawable/ic_info_white_36dp" />

<!-- Adding bottom sheet after main content -->
<include android:id="@+id/pickUpFragment" layout="@layout/fragment_pax_pick_up_2" />

我已经尝试将 FloatingButton 移动到底部工作表片段 xml 文件,它几乎可以工作,但是如果我关闭片段,浮动按钮将与底部工作表片段一起消失,并且浮动按钮也没有完全显示底部工作表被“隐藏”了。

【问题讨论】:

标签: android android-fragments maps floating-action-button bottom-sheet


【解决方案1】:

你应该在 "android:id="@+id/conteudo"" FrameLayout 中再添加两个属性:

  • app:layout_insetEdge="bottom"
  • app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

以及 FAB 中的 app:layout_dodgeInsetEdges="bottom":

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:clickable="true"
    android:focusable="true"
    app:backgroundTint="@color/white"
    app:fabSize="normal"
    app:layout_dodgeInsetEdges="bottom"
    app:srcCompat="@drawable/icon"
    />

<View
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_insetEdge="bottom"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    />

【讨论】:

    【解决方案2】:

    你可以这样做 -
    在片段的 xml 中
    线性布局(具有透明背景,可点击的错误,垂直方向)
    |-浮动按钮(下边距 8dp)
    |-你的主要LinearLayout/RelativeLayout/FrameLayout
    |- 子布局

    现在计算浮动按钮的高度
    设置底页的 peekHeight = 浮动按钮的高度 + 8dp

    现在折叠时,您的主要布局将被隐藏,但您的浮动按钮将保持可见。总是折叠你的底页不要隐藏。

    如果您想显示主要布局的某些部分而不是折叠时
    设置底页 peekHeight = 浮动按钮高度 + 该部分高度 + 8dp

    【讨论】:

    • 您是否建议在底部工作表片段中添加浮动按钮?可以发一些代码吗?
    • 是的,我的建议是一样的。
    【解决方案3】:

    这与您想要的不完全一致。但是以此为基础,您可以根据需要创建自定义行为。所以这两个组件都将在提供的行为上起作用。

    像这样创建协调器布局的自定义上移行为:

    public class CustomMoveUpBehavior extends CoordinatorLayout.Behavior {
    
        @Override
        public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
            return dependency instanceof Snackbar.SnackbarLayout; //here you can use bottom Sheet fragment in your case
        }
    
        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
            float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
            child.setTranslationY(translationY);
            return true;
        }
    
    }
    

    将您的浮动按钮更改为某个自定义视图并将此行为添加为默认行为 像这样:

    //it may extend floating Action button in your case
    @CoordinatorLayout.DefaultBehavior(CustomMoveUpBehavior.class)
    public class CustomButton extends AppCompatButton {
        public CustomButton(Context context) {
            super(context);
        }
    
        public CustomButton(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2021-02-09
      • 2022-10-05
      • 1970-01-01
      • 2018-10-30
      • 2015-06-25
      • 1970-01-01
      相关资源
      最近更新 更多