daz*_*000 3 android bottom-sheet android-viewmodel
我试图弄清楚如何将视图模型绑定到底部表,以便我可以使用视图模型上的可观察字段使底部表同时展开、折叠和隐藏。
谢谢!
您应该使用自定义BindingAdapter.
@BindingAdapter("bottomSheetBehaviorState")
public static void setState(View v, int bottomSheetBehaviorState) {
BottomSheetBehavior<View> viewBottomSheetBehavior = BottomSheetBehavior.from(v);
viewBottomSheetBehavior.setState(bottomSheetBehaviorState);
}
Run Code Online (Sandbox Code Playgroud)
将它在 xml 中绑定到您的视图:
<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>
Run Code Online (Sandbox Code Playgroud)
并在 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4030 次 |
| 最近记录: |