gpt4 book ai didi

android - 如何将 View 模型绑定(bind)到 Bottom Sheet

转载 作者:行者123 更新时间:2023-12-03 10:11:27 25 4
gpt4 key购买 nike

我试图弄清楚如何将 View 模型绑定(bind)到 Bottom Sheet ,以便我可以使用 View 模型上的可观察字段使 Bottom Sheet 展开、折叠和隐藏。

谢谢!

最佳答案

您应该使用自定义 BindingAdapter .

@BindingAdapter("bottomSheetBehaviorState")
public static void setState(View v, int bottomSheetBehaviorState) {
BottomSheetBehavior<View> viewBottomSheetBehavior = BottomSheetBehavior.from(v);
viewBottomSheetBehavior.setState(bottomSheetBehaviorState);
}

在 xml 中将其绑定(bind)到您的 View :

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
(...)
<android.support.v4.widget.NestedScrollView
android:id="@+id/group_bottom_sheet"
bottomSheetBehaviorState="@{viewModel.bottomSheetBehaviorState}"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@android:color/holo_blue_bright"
app:behavior_hideable="true"
app:behavior_peekHeight="50dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>


(...)
</layout>

并更改 ViewModel 中的状态。我的 ViewModel 中的相关代码:

public final ObservableInt bottomSheetBehaviorState = new ObservableInt(BottomSheetBehavior.STATE_HIDDEN);


@Override
public void onAction(boolean show){
bottomSheetBehaviorState.set(show? BottomSheetBehavior.STATE_COLLAPSED : BottomSheetBehavior.STATE_HIDDEN);
}

关于android - 如何将 View 模型绑定(bind)到 Bottom Sheet ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com