//获取用户输入的信息
private String getUserInput(){
return etinput.getText().toString().trim();
}
//展示获取信息成功页面
private void showSuccessPage(Account account){
tvinformation.setText("用户账号:"+account.getName()+"用户等级:"+account.getLevel());
}
//展示获取信息失败页面
private void showFailedPage(){
tvinformation.setText("获取数据失败");
}
//查询用户数据
private void getAccountData(String accountName, MCallBack callBack){
Random random = new Random();
boolean isSuccess = random.nextBoolean();
if (isSuccess){
Account account = new Account();
account.setName(accountName);
account.setLevel(100);
callBack.onSuccess(account);
}else {
callBack.onFailed();
}
}
@Override
public void onClick(View view) {
String userInput = getUserInput();
getAccountData(userInput, new MCallBack() {
@Override
public void onSuccess(Account account) {
showSuccessPage(account);
}
@Override
public void onFailed() {
showFailedPage();
}
});
}
public interface MCallBack {
void onSuccess(Account account);
void onFailed();
}
//Account Bean类
private String name;//姓名
private int level;//等级
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
//Model层
public void getAccountData(String accountName, MCallBack callBack){
Random random = new Random();
boolean isSuccess = random.nextBoolean();
if (isSuccess){
Account account = new Account();
account.setName(accountName);
account.setLevel(100);
callBack.onSuccess(account);
}else {
callBack.onFailed();
}
}
//Controller层
private String getUserInput(){
return etinput.getText().toString().trim();
}
private void showSuccessPage(Account account){
tvinformation.setText("用户账号:"+account.getName()+"用户等级:"+account.getLevel());
}
private void showFailedPage(){
tvinformation.setText("获取数据失败");
}
@Override
public void onClick(View view) {
mMvcModel.getAccountData(getUserInput(), new MCallBack() {
@Override
public void onSuccess(Account account) {
showSuccessPage(account);
}
@Override
public void onFailed() {
showFailedPage();
}
});
}
//Model层负责提供数据方面的功能
public void getAccountData(String accountName, MCallBack callBack){
Random random = new Random();
boolean isSuccess = random.nextBoolean();
if (isSuccess){
Account account = new Account();
account.setName(accountName);
account.setLevel(100);
callBack.onSuccess(account);
}else {
callBack.onFailed();
}
}
//采用实现接口的方式实现View层面的功能
public interface MVPView {
String getUserInput();
void showSuccessPage(Account account);
void showFailedPage();
}
//中间层Presenter
private MVPView mMVPView;
private MVPModel mMVPModel;
public MVPPresenter(MVPView mMVPView) {
this.mMVPView = mMVPView;
mMVPModel = new MVPModel();
}
public void getData(String accountName){
mMVPModel.getAccountData(accountName, new MCallBack() {
@Override
public void onSuccess(Account account) {
mMVPView.showSuccessPage(account);
}
@Override
public void onFailed() {
mMVPView.showFailedPage();
}
});
}
//Activity中Model与View不再直接通信,通过Presenter来实现。继承View接口层
@Override
public void onClick(View view) {
mMVPPresenter.getData(getUserInput());
}
@Override
public String getUserInput() {
return etinput.getText().toString().trim();
}
@Override
public void showSuccessPage(Account account) {
tvinformation.setText("用户账号:"+account.getName()+"用户等级:"+account.getLevel());
}
@Override
public void showFailedPage() {
tvinformation.setText("获取数据失败");
}
1.接口规范化(封装父类接口以减少接口的使用量)。
2.使用MVP插件自动生成MVP的代码。
3.对于一些简单的页面,可以选择不使用框架。
4.根据项目复杂度,部分模块可以选择不使用接口。
1.启用DataBinding
//编辑app目录下的build.gradle文件,添加下面的内容。
android {
...
dataBinding {
enabled = true
}
}
2.修改布局文件为DataBinding布局
<?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="account"
type="com.yhj.framedemo.bean.Account" />
<variable
name="activity"
type="android.app.Activity" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".databinding.DemoActivity">
<TextView
android:id="@+id/tv_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:text="@{account.name+'|'+account.level}" />
<Button
android:id="@+id/bt_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:onClick="@{activity.onClick()}"
android:text="等级+1" />
</LinearLayout>
</layout>
ActivityMvvmBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_mvvm);
binding.etInput.setText("省略findViewById()");
3.数据绑定
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_demo);
account = new Account();
account.setName("Tom");
account.setLevel(100);
binding.setAccount(account);
binding.setActivity(this);
}
public void onClick(View view){
Toast.makeText(this, "点击了!", Toast.LENGTH_SHORT).show();
int level = account.getLevel();
account.setLevel(level+1);
//binding.setAccount(account);
}
public class Account extends BaseObservable {
private String name;//姓名
private int level;//等级
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Bindable
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
notifyPropertyChanged(BR.level);
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- 首先是DataBinding布局-->
<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="viewModel"
type="com.yhj.framedemo.mvvm.MVVMViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".mvvm.MVVMActivity">
<EditText
android:hint="请输入要查询的账号"
android:text="@={viewModel.userInput}"
android:layout_marginTop="20dp"
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:id="@+id/bt_get"
android:onClick="@{viewModel.getData}"
android:text="获取账号信息"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:id="@+id/tv_information"
android:text="@{viewModel.result}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</layout>
//Model层数据处理
public void getAccountData(String accountName, MCallBack callBack){
Random random = new Random();
boolean isSuccess = random.nextBoolean();
if (isSuccess){
Account account = new Account();
account.setName(accountName);
account.setLevel(100);
callBack.onSuccess(account);
}else {
callBack.onFailed();
}
}
//ViewModel层
public class MVVMViewModel extends BaseObservable {
private String result;
private String userInput;
@Bindable
public String getUserInput() {
return userInput;
}
public void setUserInput(String userInput) {
this.userInput = userInput;
notifyPropertyChanged(BR.userInput);
}
private final MVVMModel mvvmModel;
private MVVMModel mvvmModel1;
private ActivityMvvmBinding binding;
@Bindable
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
notifyPropertyChanged(BR.result);
}
//一般需要传入Application对象,方便在ViewModel中使用Application
//比如SharedPreference
public MVVMViewModel(Application application) {
mvvmModel = new MVVMModel();
}
public void getData(View view) {
mvvmModel.getAccountData(userInput, new MCallBack() {
@Override
public void onSuccess(Account account) {
String info = account.getName() + "|" + account.getLevel();
setResult(info);
}
@Override
public void onFailed() {
setResult("获取失败!!!");
}
});
}
}
//View层Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMvvmBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_mvvm);
MVVMViewModel mvvmViewModel = new MVVMViewModel(getApplication());
binding.setViewModel(mvvmViewModel);
}