Observable DataBinding ObservableInt 与 LiveData 在配置更改时具有不同的结果

Observable DataBinding ObservableInt vs. LiveData with different results On Confguration change

我正在测试 DataBinding observable 在使用 LiveDataObservableInt.

时的行为

我有一个简单的布局,带有一个按钮,该按钮触发我存储在 ViewModel 中的 LiveDataObservableInt 变量的计数器,我使用 [=20= 更新它们的值]

LiveData & ObservableInt 变量都正常计数;但是当设备配置发生变化时(我的测试屏幕旋转),ObservableInt countinues 会随着按钮点击而计数,尽管 LiveData 会忽略一两个计数。

下面的gif会说明更多

我的问题是如何解决这个问题? 我主要需要一个 LiveData 进一步 Transformations

布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="viewmodel"
            type="com.example.android.databindingobservableintlivedata.MyViewModel" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <TextView
                android:id="@+id/observableint_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ObservableInt: " />

            <TextView
                android:id="@+id/observableint_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                app:textValueForObservableInt="@{viewmodel.countObservableInt}" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:gravity="center">

            <TextView
                android:id="@+id/livedata_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LiveData: " />

            <TextView
                android:id="@+id/livedata_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                app:textValueForLiveData="@{viewmodel.countLiveData}" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="16dp"
            android:onClick="@{() -> viewmodel.onCount()}"
            android:text="Count" />


    </LinearLayout>
</layout>

Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);

        mBinding.setViewmodel(viewModel);
        mBinding.setLifecycleOwner(this); 

    }
}

ViewModel

public class MyViewModel extends ViewModel {

    public MutableLiveData<Integer> countLiveData = new MutableLiveData<>(0);
    public ObservableInt countObservableInt = new ObservableInt(0);

    public void onCount() {
        countObservableInt.set(countObservableInt.get() + 1);
        int value = countLiveData.getValue() == null ? 0 : countLiveData.getValue();
        countLiveData.postValue(value + 1);
    }

}

绑定适配器

public class BindingAdapters {

    @BindingAdapter("textValueForObservableInt")
    public static void bindObservableIntInText(TextView text, ObservableInt value) {
        text.setText(String.valueOf(value.get()));
    }

    @BindingAdapter("textValueForLiveData")
    public static void bindLiveDataIntegerInText(TextView text, MutableLiveData<Integer> value) {
        text.setText(String.valueOf(value.getValue()));
    }

}

Post value,共 LiveData

基本上这个方法表明,任何给 LiveData 的值都可以 be/should 从后台线程调用 (主线程除外) 会反映具有更新值的主线程。

意思是,如果您连续两次调用 LiveData 大约 postValue(),那么只会发送最后一个值!

在你的情况下,countLiveData.postValue(value + 1)如果调用此方法过于频繁,此行会影响 post-值增量,如果只更新最后一个值而不是连续的值。

Set Value,共 LiveData

此方法要求必须从主线程进行调用,因此无论您进行了多少次调用,UI 每次都会得到 reflected/updated。 None 的中间调用被丢弃。

TL;DR

如果您要从后台线程获取 update/set 值,则使用 postValue() 方法,否则在主线程上使用 setValue()


It takes a while for device rotation when I have continuous hits on the button, and suddenly rotate the device.

原因是当您在旋转设备之前在主线程上调用 setValue() 时,每次调用都必须连续更新 UI,导致旋转延迟 (持有 UI 配置更改发生前的更新)。这就是为什么它 lags/delay 在所有 setValue() 调用完成之前轮换 UI 更新 (您的特定情况下的这一行:app:textValueForLiveData="@{viewmodel.countLiveData}"