我的目标:我想使用 Transformations.distinctUntilChanged() 方法创建一个应用程序,该应用程序在每次更新 LiveData 对象时显示一条 toast 消息。LiveData 对象是一个简单的自定义 Integer 对象 (MyInteger),每次单击按钮时递增 1。当 myInteger 超过 5 时,应用程序应该停止显示 toast 消息。要实现这个 >5 条件,应用程序必须重写 MyInteger 类的 equals() 方法(在 distinctUntilChanged() 类中调用 equals() 方法)。
问题:在下面的代码中,currentValue 和 previousValue 字段始终相同(第一次单击按钮除外)。因此,在 MainActivity 中未调用 onChanged,并且从第二次单击开始不会显示 Toast 消息。有关当前结果与我期望应该发生的情况,请参见下文。
请帮忙。同样,应用程序必须同时使用 distinctUntilChanged() 和覆盖的 equals() 方法。
Current result:
First time button clicked: currentValue=1 and previousValue=null, Toast Message shows.
Second time button clicked: currentValue=2 and previousValue=2, Toast Message does not show.
Third time button clicked: currentValue=3 and previousValue=3, Toast Message does not show.
Fourth time button clicked: currentValue=4 and previousValue=4, Toast Message does not show.
and so on...
Desired result:
First time button clicked: currentValue=1 and previousValue=null, Toast Message shows.
Second time button clicked: currentValue=2 and previousValue=1, Toast Message shows.
Third time button clicked: currentValue=3 and previousValue=2, Toast Message shows.
Fourth time button clicked: currentValue=4 and previousValue=3, Toast Message shows.
and so on...
块引用
public class MainActivity extends AppCompatActivity {
private MainActivityViewModel mMainActivityViewModel;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.my_button);
MyInteger myInteger = new MyInteger(0);
mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myInteger.incrementByOne();
mMainActivityViewModel.getLiveData().setValue(myInteger);
}
});
Transformations.distinctUntilChanged(mMainActivityViewModel.getLiveData()).observe(this, new Observer<MyInteger>() {
@Override
public void onChanged(MyInteger myInteger) {
Log.wtf("Transformations onChanged:", String.valueOf(myInteger.integer));
Toast.makeText(MainActivity.this, "Transformations onChanged:"+String.valueOf(myInteger.integer), Toast.LENGTH_SHORT).show();
}
});
}
}
块引用
public class MainActivityViewModel extends AndroidViewModel {
public MutableLiveData<MyInteger> myIntegerMutableLiveData;
public MainActivityViewModel(@NonNull Application application) {
super(application);
}
public MutableLiveData<MyInteger> getLiveData() {
if (myIntegerMutableLiveData == null) {
myIntegerMutableLiveData = new MutableLiveData<>();
}
// MediatorLiveData transformedLiveData = (MediatorLiveData) Transformations.distinctUntilChanged(myIntegerMutableLiveData);
return myIntegerMutableLiveData;
}
}
块引用
public class MyInteger {
public int integer;
public MyInteger(Integer integer) {
this.integer = integer;
}
public void incrementByOne() {
integer++;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyInteger myInteger = (MyInteger) o;
if ((myInteger.integer <= 5) && (integer == myInteger.integer)) {return true;}
if ((myInteger.integer <= 5) && (integer != myInteger.integer)) {return false;}
return (myInteger.integer > 5);
}
@Override
public int hashCode() {
return Objects.hash(integer);
}
}