MVVM模式就相当于把ViewModel改名为Presener,其他基本相同。在ViewModel中处理数据逻辑,调用数据请求。但它和MVP的区别是可以双向绑定,ViewModel中数据的改变可以直接反应在View上,而View的修改也可以影响ViewModel,这样就省去Presenter和View之间的大量的调用接口。
谷歌官方提供了一个ViewModel类。这个类主要用来保存View的数据,一般在Activity的onCreate()方法中初始化ViewModel,但如果第二次调用onCreate(),那获取到的ViewModel依然是第一次生成的那个实例,这就保证了数据的连贯。一般使用的场景为当屏幕旋转时,Activity会重新调用onCreate()方法,但这时ViewModel依然能保存之前的数据。但Activity完全finish时,ViewModel才会被清空。
使用ViewModel还有其他好处,比如方便同一个Activity中不同的Fragment直接的通信,可以避免内存泄漏等等。
以下是ViewModel的基本使用方法,来自官网:
public class MyViewModel extends ViewModel {
private MutableLiveData<List<User>> users;
public LiveData<List<User>> getUsers() {
if (users == null) {
users = new MutableLiveData<List<Users>>();
loadUsers();
}
return users;
}
private void loadUsers() {
// Do an asyncronous operation to fetch users.
}
}
public class MyActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
// Create a ViewModel the first time the system calls an activity's onCreate() method.
// Re-created activities receive the same MyViewModel instance created by the first activity.
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
model.getUsers().observe(this, users -> {
// update UI
});
}
}
ViewModel一般和LiveData一起用。LiveData中的数据发生改变时,能发送通知给观察者,观察者就能修改相应的View。这也使得MVVM模式获取数据时可以直接返回一个结果,而不用等网络请求数据成功后再通过回调返回数据,因为在回调中修改数据之后,LiveData会自动通知大家这个数据被修改了。
public abstract class NetworkBoundResource<ResultType, RequestType> {
private final MediatorLiveData<Resource<ResultType>> result = new MediatorLiveData<>();
@MainThread
NetworkBoundResource() {
result.setValue(Resource.loading(null));
LiveData<ResultType> dbSource = loadFromDb();
result.addSource(dbSource, data -> {
result.removeSource(dbSource);
if (shouldFetch(data)) {
fetchFromNetwork(dbSource);
} else {
result.addSource(dbSource,
newData -> result.setValue(Resource.success(newData)));
}
});
}
private void fetchFromNetwork(final LiveData<ResultType> dbSource) {
LiveData<ApiResponse<RequestType>> apiResponse = createCall();
// we re-attach dbSource as a new source,
// it will dispatch its latest value quickly
result.addSource(dbSource,
newData -> result.setValue(Resource.loading(newData)));
result.addSource(apiResponse, response -> {
result.removeSource(apiResponse);
result.removeSource(dbSource);
//noinspection ConstantConditions
if (response.isSuccessful()) {
saveResultAndReInit(response);
} else {
onFetchFailed();
result.addSource(dbSource,
newData -> result.setValue(
Resource.error(response.errorMessage, newData)));
}
});
}
@MainThread
private void saveResultAndReInit(ApiResponse<RequestType> response) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
saveCallResult(response.body);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// we specially request a new live data,
// otherwise we will get immediately last cached value,
// which may not be updated with latest results received from network.
result.addSource(loadFromDb(),
newData -> result.setValue(Resource.success(newData)));
}
}.execute();
}
public final LiveData<Resource<ResultType>> getAsLiveData() {
return result;
}
}
class UserRepository {
Webservice webservice;
UserDao userDao;
public LiveData<Resource<User>> loadUser(final String userId) {
return new NetworkBoundResource<User,User>() {
@Override
protected void saveCallResult(@NonNull User item) {
userDao.insert(item);
}
@Override
protected boolean shouldFetch(@Nullable User data) {
return rateLimiter.canFetch(userId) && (data == null || !isFresh(data));
}
@NonNull @Override
protected LiveData<User> loadFromDb() {
return userDao.load(userId);
}
@NonNull @Override
protected LiveData<ApiResponse<User>> createCall() {
return webservice.getUser(userId);
}
}.getAsLiveData();
}
}
除此之外谷歌还提供了其他的架构组件,包括Lifecycle和Room等,可以查看官网的介绍。