0

我有:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <import type="android.view.View" />
        <variable name="vm" type="com.my.package.viewmodel.SplashActivityVM" />
    </data>

    <android.support.v7.widget.LinearLayoutCompat
        bla...>

        <TextView
            bla...
            android:text='@{vm.status}'
            bla... />
bla...

使用activity.java:

@Override
protected void onCreate(Bundle savedInstanceState) { bla... }

private void observeStatus(SplashActivityVM viewModel) {
        viewModel.getStatus().observe(this, status -> {
            if (status != null) {
                mBinding.setVm(viewModel);
            } else { }
            mBinding.executePendingBindings();
        });
    }

和我的视图模型:

public class SplashActivityVM extends AndroidViewModel implements ViewModel {

    private final MutableLiveData<SplashState> mObservableStatus = new MutableLiveData<>();
    public MutableLiveData<SplashState> getStatus() { return mObservableStatus; }

    public SplashActivityVM(@NonNull Application application) { super(application); }

// bla...

我收到以下错误:

msg:在 android.widget.TextView 上找不到参数类型为 android.arch.lifecycle.MutableLiveData 的属性“android:text”的设置器。

怎么了?

4

1 回答 1

0
  1. 您需要为您的 SplashState自定义绑定适配器

    文本视图适配器.kt

@BindingAdapter("android:text")
fun setText(view: TextView, state: SplashState) {
    view.text = state.toString()
}
  1. viewLifecycleOwner设置为 livedata 的数据绑定
@Override
protected void onCreate(Bundle savedInstanceState) { bla... 

mBinding.setLifecycleOwner(this);
}
  1. 移除observeStatus方法,设置VM绑定
@Override
protected void onCreate(Bundle savedInstanceState) { bla... 

mBinding.setLifecycleOwner(this);
mBinding.setvm(viewModel);
}
于 2019-08-11T06:36:50.733 回答