android mvvm封装
MVVM(Model-View-ViewModel)是一种基于数据绑定的架构模式,它将应用程序分为三个部分:模型(Model)、视图(View)和视图模型(ViewModel)。在MVVM架构中,视图和模型之间是双向绑定的,而视图模型则扮演着连接视图和模型的桥梁。
Android开发中,MVVM架构被广泛应用,它能够帮助我们更好地组织代码、提高代码的可读性和可维护性。在MVVM架构中,我们通常会将数据绑定库和ViewModel进行封装,以便在项目中更好地使用。
下面我们将详细介绍如何在Android中封装MVVM架构。
一、封装数据绑定库
数据绑定库是MVVM架构中重要的一部分,它可以帮助我们将数据与视图进行绑定。在Android中,Google提供了自己的数据绑定库,我们可以在项目中引入该库,并对其进行封装。
1.在项目的build.gradle文件中添加以下依赖:
```
android {
...
dataBinding {
enabled = true
}
}
dependencies {
...
implementation 'com.android.databinding:library:4.0.1'
}
```
2.新建一个BaseBindingAdapter类,用于绑定数据和视图:
```
public abstract class BaseBindingAdapter
protected List
protected int mLayoutId;
public BaseBindingAdapter(List
mDataList = dataList;
mLayoutId = layoutId;
}
@NonNull
@Override
public BaseBindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), mLayoutId, parent, false);
return new BaseBindingViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull BaseBindingViewHolder holder, int position) {
holder.getBinding().setVariable(BR.item, mDataList.get(position));
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return mDataList.size();
}
public static class BaseBindingViewHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;
public BaseBindingViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public ViewDataBinding getBinding() {
return binding;
}
}
}
```
3.在ViewModel中定义绑定数据的方法:
```
public class MainViewModel extends ViewModel {
public MutableLiveData> userListLiveData = new MutableLiveData<>();
public void getUserList() {
List
userList.add(new User("张三", 20));
userList.add(new User("李四", 21));
userList.add(new User("王五", 22));
userListLiveData.setValue(userList);
}
}
```
4.在Activity或Fragment中使用绑定数据的方法:
```
public class MainActivity extends AppCompatActivity {
private MainViewModel mViewModel;
private ActivityMainBinding mBinding;
private BaseBindingAdapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
mBinding.setViewModel(mViewModel);
mBinding.setLifecycleOwner(this);
mAdapter = new BaseBindingAdapter<>(new ArrayList<>(), R.layout.item_user);
mBinding.userList.setAdapter(mAdapter);
mViewModel.userListLiveData.observe(this, userList -> mAdapter.setDataList(userList));
mViewModel.getUserList();
}
}
```
二、封装ViewModel
在MVVM架构中,ViewModel扮演着连接视图和模型的桥梁,它负责处理视图中的数据和事件,并将结果传递给模型。在Android中,我们可以使用ViewModelProvider来创建ViewModel,并将其与Activity或Fragment进行绑定。
1.新建一个BaseViewModel类,用于封装ViewModel的公共方法:
```
public abstract class BaseViewModel extends ViewModel {
private CompositeDisposable mCompositeDisposable;
public BaseViewModel() {
mCompositeDisposable = new CompositeDisposable();
}
protected void addDisposable(Disposable disposable) {
mCompositeDisposable.add(disposable);
}
@Override
protected void onCleared() {
super.onCleared();
mCompositeDisposable.clear();
}
}
```
2.在MainViewModel中继承BaseViewModel,并定义自己的方法:
```
public class MainViewModel extends BaseViewModel {
public MutableLiveData> userListLiveData = new MutableLiveData<>();
public void getUserList() {
Disposable disposable = Observable.create((ObservableOnSubscribe>) emitter -> {
List
userList.add(new User("张三", 20));
userList.add(new User("李四", 21));
userList.add(new User("王五", 22));
emitter.onNext(userList);
emitter.onComplete();
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(userListLiveData::setValue);
addDisposable(disposable);
}
}
```
3.在Activity或Fragment中使用ViewModel:
```
public class MainActivity extends AppCompatActivity {
private MainViewModel mViewModel;
private ActivityMainBinding mBinding;
private BaseBindingAdapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
mBinding.setViewModel(mViewModel);
mBinding.setLifecycleOwner(this);
mAdapter = new BaseBindingAdapter<>(new ArrayList<>(), R.layout.item_user);
mBinding.userList.setAdapter(mAdapter);
mViewModel.userListLiveData.observe(this, userList -> mAdapter.setDataList(userList));
mViewModel.getUserList();
}
}
```
以上就是在Android中封装MVVM架构的方法,通过封装数据绑定库和ViewModel,我们可以更好地组织和管理代码,提高代码的可读性和可维护性。