使用谷歌数据绑定对视图宽度进行动画处理



我想在我的视图模型中布尔值更改时对视图宽度进行动画处理...但我不知道如何绑定它。

我有这样的看法:

<EditText
     android:id="@+id/help_search_et"
     android:layout_width="48dp"
     android:layout_height="match_parent"
     android:background="@drawable/border_search_help_txt"
     android:hint="@string/hint_how_can_we_help"
     android:singleLine="true" />

还有一个视图模型:

public class SomethingViewModel extends BaseViewModel {
     private boolean isSearchEnabled;
     void handleSearchRequest(){
        if(isSearchEnabled) {
            isSearchEnabled = false;
            /* I need to make the EditText expand */
        } else {
            isSearchEnabled = true;
            /* I need to make the EditText colapse */
        }
    }
}

由于我正在使用 MVVM,因此无法引用视图...所以我无法在视图中触发动画,我需要数据绑定来为我执行此操作......但在我看来,我不知道如何触发这个动画。

我不需要宽度动画的帮助,只需要数据绑定部分。

有几种方法可以做到这一点。首先想到的是使用转换:

binding.addOnRebindCallback(new OnRebindCallback() {
    @Override
    public boolean onPreBind(ViewDataBinding binding) {
        TransitionManager.beginDelayedTransition(
                (ViewGroup)binding.getRoot());
        return super.onPreBind(binding);
    }
});

另一种选择是创建一个绑定适配器:

@BindingAdapter("isExpanded")
public void setExpanded(View view, boolean isExpanded) {
    // expand or collapse View, depending on isExpanded
}

您必须将其绑定到布局中:

<View app:isExpanded="@{viewModel.isSearchEnabled}" .../>

最新更新