依赖:implementation "android.arch.lifecycle:extensions:1.1.1"
public class FruitViewModel extends ViewModel {
private MutableLiveData<Fruit> mApple;
public LiveData<Fruit> getApple(){
if(mApple==null){
mApple = new MutableLiveData<>();
mBanner = new MutableLiveData<>();
mGrape = new MutableLiveData<>();
loadFruit();
}
return mApple;
}
private void loadFruit() {
final Fruit apple = new Fruit();
apple.setName("苹果");
apple.setPrice(5);
apple.setImg(R.drawable.apple);
mApple.postValue(apple);
}
}