Android以XML设置底部工作表状态

时间:2019-04-15 03:32:20

标签: android android-layout bottom-sheet

XML的底部工作表具有几种状态:STATE_COLLAPSED,STATE_EXPANDED,STATE_HIDDEN等。

如何使用可观察的方式在XML中设置集合?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior"
    <!-- Example of State -->
    app:bottom_state="@{viewModel.state}"
    >
</LinearLayout>

很明显,底部状态不是正确的名称,但我试图弄清楚是什么。

1 个答案:

答案 0 :(得分:2)

没有直接的xml值可用于底页状态。但是,如果要在数据绑定中执行此操作,则还有另一种选择。为此创建一个绑定适配器并使用它。

 @BindingAdapter("bottomSheetState")
    fun bindingBottomSheet(container: LinearLayout, state:Int) {
        val behavior=BottomSheetBehavior.from(container)
        behavior.state = state
    }

现在可以在xml中使用它

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="340dp"
            app:behavior_hideable="true"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            app:behavior_peekHeight="80dp"
            app:bottomSheetState="@{model.uiState.bottomSheetState}">