赞
踩
控制层(Controller)
业务逻辑处理
image.png
- public class MvcModel {
- String mId;
- MvcActivity mActivity;
-
- public MvcModel (MvcActivity activity) {
- this.mActivity = activity;
- }
- public void loadModel (){
- //模拟从网络或者本地获取数据
- mId = "20170923";
- mActivity.updateUI(this);
- }
- }
- public class MvcActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- //省略
- final MvcController controller = new MvcController(this);
- mFindBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- controller.loadData();
- }
- });
- }
-
- public void updateUI (MvcModel model) {
- mID.setText(model.mId);
- }
- }
- public class MvcController {
- MvcActivity mActivity;
- MvcModel mModel;
-
- MvcController (MvcActivity activity) {
- this.mActivity = activity;
- }
-
- public void loadData (){
- mModel = new MvcModel(mActivity);
- mModel.loadModel();
- }
- }
image.png
- public interface MvpContract {
- interface View {
- void updateUI();
- }
-
- interface Model {
- void loadModel();
- }
-
- interface Presenter {
- void loadData ();
- }
- }
- public class MvpModel implements MvpContract.Model{
- String mId;
- OnGetListener mListener;
-
- public MvpModel (OnGetListener listener) {
- this.mListener = listener;
- }
- @Override
- public void loadModel (){
- //模拟从网络或者本地获取数据
- mId = "20170923";
- if (mListener != null) {
- mListener.onSuccess(this);
- }
- }
- interface OnGetListener {
- void onSuccess (MvpModel model);
- }
- }
- public class MvpActivity extends AppCompatActivity implements MvpContract.View {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- //省略
- final MvpPresenter presenter = new MvpPresenter(this);
- mFindBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- presenter.loadData();
- }
- });
- }
- @Override
- public void updateUI (MvpModel model) {
- mID.setText(model.mId);
- }
- }
- public class MvpPresenter implements MvpContract.Presenter {
- MvpContract.View mView;
- MvpContract.Model mModel;
-
- MvpPresenter (MvpContract.View view) {
- this.mView= view;
- }
- @Override
- public void loadData (){
- mModel = new MvpModel(new MvpModel.OnGetListener() {
- @Override
- public void onSuccess(MvpModel model) {
- mView.updateUI(model);
- }
- });
- mModel.loadModel ();
- }
- }
视图-模型层(ViewModel)
负责View和Model之间的通信,以此分离视图和数据。
image.png
- public class MvvmModel {
- String mId;
- OnGetListener mListener;
-
- public MvvmModel (OnGetListener listener) {
- this.mListener = listener;
- }
- public void loadModel (){
- //模拟从网络或者本地获取数据
- mId = "20170923";
- if (mListener != null) {
- mListener.onSuccess(this);
- }
- }
- interface OnGetListener {
- void onSuccess (MvvmModel model);
- }
- }
- public class MvvmViewModel extends BaseObservable{
- public ObservableField<String> mId = new ObservableField<>();
- public MvvmViewModel() {
-
- }
- public void doGetId(View view){
- loadData();
- }
- public void loadData() {
- MvvmModel model = new MvvmModel(new MvvmModel.OnGetListener() {
- @Override
- public void onSuccess(MvvmModel model) {
- mId.set(model.mId);
- }
- });
- model.loadModel();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools">
- <data>
- <variable name="viewModel" type="com.liujc.mvvm.MvvmViewModel"/>
- </data>
- <LinearLayout
- <TextView
- android:id="@+id/id"
- android:layout_width="match_parent"
- android:layout_height="20dp"
- android:text="@{viewModel.mId}"/>
- <Button
- android:id="@+id/findBtn"
- android:onClick="@{viewModel.doGetId}"
- android:text="获取Id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </layout>
- public class MvvmActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- ActivityMvvmBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_mvvm);
-
- final MvvmViewModel viewModel = new MvvmViewModel();
- binding.setViewModel(viewModel);
- }
-
- }
DataBinding是2015年谷歌 I/O大会上介绍了一个数据绑定框架,以前我们可能需要在每个Activity里写很多的findViewById,不仅麻烦,还增加了代码的耦合性,如果我们使用DataBinding,就可以抛弃那么多的findViewById,省时省力。
双向绑定的概念让传统的布局文件由被动转为主动,数据驱动UI,而且View与ViewModel实现了完美的解耦,这也解决了MVP模式下的缺点。
如果你看到了这里,觉得文章写得不错就给个赞呗!欢迎大家评论讨论!如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足,定期免费分享技术干货。谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。