如何在MVP中使用ViewModel?

时间:2018-11-21 10:34:32

标签: android-databinding android-viewmodel android-mvp

我正在开发一个android应用程序,并且正在使用MVP作为设计模式。我想将ViewModel与MVP一起使用。

注意:我正在使用数据绑定。     示例ViewModel CLASS

public class ViewModel extends BaseObservable{
public String username;
public String password;
}

--------------------------------------------------------------
EXAMPLE PRESENTER CLASS    

public class Presenter{
public View mView; 
public ViewModel mViewModel;
}

--------------------------------------------------------------

EXAMPLE LAYOUT XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

    <variable
        name="presenter"
        type="ui.login.LoginPresenterImpl" />
</data>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="LOG IN"
            android:text=@={presenter.viewModel.username}/>

--------------------------------------------------------------

EXAMPLE FRAGMENT CLASS

public class Fragment{
public Presenter myPresenter;
binding.get.setPresenter(myPresenter);
}

这是正确的结构吗?

0 个答案:

没有答案