android MVP 单个activity 拆分成多个view, 当A view 点击按钮后,B /C view 继承的接口同时可以回调到A view的数据
MvpActivity 单个页面 代码
package com.example.demo.mvp.view;
import android.app.Activity;
import android.os.Bundle;
import com.example.demo.databinding.ActivityMvpBinding;
import com.example.demo.mvp.presenter.MvpPresenterClass;
public class MvpActivity extends Activity {
// viewBinding数据绑定节约了findViewByid
private ActivityMvpBinding binding;
// 拆分的主view
private MainView mainView;
// P层
private MvpPresenterClass presenterClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 数据绑定
binding = ActivityMvpBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mainView = new MainView(binding, this);
presenterClass = new MvpPresenterClass(this);
mainView.updateMainView(presenterClass);
}
}
MainVIew
package com.example.demo.mvp.view;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.example.demo.R;
import com.example.demo.databinding.ActivityMvpBinding;
import com.example.demo.mvp.presenter.MvpPresenterClass;
import com.example.demo.mvp.MvpView;
import java.util.ArrayList;
import java.util.List;
public class MainView implements MvpView, View.OnClickListener {
private final ActivityMvpBinding binding;
private final static String TAG = MainView.class.getName();
private final Context context;
private MvpPresenterClass mvpPresenterClass;
private ShowView showView;
private TimeView timeView;
public MainView(ActivityMvpBinding binding, Context context) {
this.binding = binding;
this.context = context;
initView();
}
private void initView() {
binding.mainView.mvp1.setOnClickListener(this);
binding.mainView.mvp2.setOnClickListener(this);
}
@Override
public void bu1Status(int status) {
Log.d(TAG, "mvp bu1Status: " + status);
}
@Override
public void bu2Status(int status) {
Log.d(TAG, "mvp bu2Status: " + status);
}
@Override
public void bu3Status(int status) {
Log.d(TAG, "mvp bu3Status: " + status);
}
@Override
public void bu4Status(int status) {
Log.d(TAG, "mvp bu4Status: " + status);
}
@Override
public void bu5Status(int status) {
Log.d(TAG, "mvp bu5Status: " + status);
}
@Override
public void bu6Status(int status) {
Log.d(TAG, "mvp bu6Status: " + status);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mvp1:
mvpPresenterClass.setButton1(1);
break;
case R.id.mvp2:
mvpPresenterClass.setButton2(2);
break;
default:
break;
}
}
public void updateMainView(MvpPresenterClass presenterClass) {
this.mvpPresenterClass = presenterClass;
showView = new ShowView(binding.showView, context);
timeView = new TimeView(binding.timeView, context);
showView.updateTimeView(presenterClass);
timeView.updateTimeView(presenterClass);
mvpPresenterClass.registerCallback(this);
}
}
ShowVIew
package com.example.demo.mvp.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.example.demo.R;
import com.example.demo.databinding.ShowViewBinding;
import com.example.demo.mvp.presenter.MvpPresenterClass;
import com.example.demo.mvp.MvpView;
public class ShowView implements MvpView, View.OnClickListener {
private static final String TAG = ShowView.class.getSimpleName();
private final ShowViewBinding binding;
private MvpPresenterClass presenterClass;
private Context context;
public ShowView(ShowViewBinding binding, Context context) {
this.context = context;
this.binding = binding;
initView();
}
public void updateTimeView(MvpPresenterClass presenterClass) {
this.presenterClass = presenterClass;
presenterClass.registerCallback(this);
}
private void initView() {
binding.mvp3.setOnClickListener(this);
binding.mvp4.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mvp3:
presenterClass.setButton3(3);
break;
case R.id.mvp4:
presenterClass.setButton4(4);
break;
default:
break;
}
}
@Override
public void bu1Status(int status) {
Log.d(TAG, "mvp bu1Status: " + status);
}
@Override
public void bu2Status(int status) {
Log.d(TAG, "mvp bu3Status: " + status);
}
@Override
public void bu3Status(int status) {
Log.d(TAG, "mvp bu3Status: " + status);
}
@Override
public void bu4Status(int status) {
Log.d(TAG, "mvp bu4Status: " + status);
}
@Override
public void bu5Status(int status) {
Log.d(TAG, "mvp bu5Status: " + status);
}
@Override
public void bu6Status(int status) {
Log.d(TAG, "mvp bu6Status: " + status);
}
}
TimeView
package com.example.demo.mvp.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.example.demo.R;
import com.example.demo.databinding.TimeViewBinding;
import com.example.demo.mvp.presenter.MvpPresenterClass;
import com.example.demo.mvp.MvpView;
public class TimeView implements MvpView, View.OnClickListener {
private static final String TAG = MvpView.class.getSimpleName();
private final TimeViewBinding binding;
private MvpPresenterClass presenterClass;
private Context context;
public TimeView(TimeViewBinding binding, Context context) {
this.context = context;
this.binding = binding;
initView();
}
private void initView() {
binding.mvp5.setOnClickListener(this);
binding.mvp6.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mvp5:
presenterClass.setButton5(5);
break;
case R.id.mvp6:
presenterClass.setButton6(6);
break;
default:
break;
}
}
@Override
public void bu1Status(int status) {
Log.d(TAG, "mvp bu1Status: " + status);
}
@Override
public void bu2Status(int status) {
Log.d(TAG, "mvp bu3Status: " + status);
}
@Override
public void bu3Status(int status) {
Log.d(TAG, "mvp bu3Status: " + status);
}
@Override
public void bu4Status(int status) {
Log.d(TAG, "mvp bu4Status: " + status);
}
@Override
public void bu5Status(int status) {
Log.d(TAG, "mvp bu5Status: " + status);
}
@Override
public void bu6Status(int status) {
Log.d(TAG, "mvp bu6Status: " + status);
}
public void updateTimeView(MvpPresenterClass presenterClass) {
this.presenterClass = presenterClass;
presenterClass.registerCallback(this);
}
}
MvpModelListener model 的接口
package com.example.demo.mvp.model;
import com.example.demo.mvp.FunctionListener;
public interface MvpModelListener {
/**
* 获取but1数据
*
* @param status 入参
* @param listener 接口回调
*/
void getButton1Status(int status, FunctionListener listener);
/**
* 获取but2数据
*
* @param status 入参
* @param listener 接口回调
*/
void getButton2Status(int status, FunctionListener listener);
/**
* 获取but3数据
*
* @param status 入参
* @param listener 接口回调
*/
void button3Status(int status, FunctionListener listener);
/**
* 获取but4数据
*
* @param status 入参
* @param listener 接口回调
*/
void button4Status(int status, FunctionListener listener);
/**
* 获取but5数据
*
* @param status 入参
* @param listener 接口回调
*/
void button5Status(int status, FunctionListener listener);
/**
* 获取but6数据
*
* @param status 入参
* @param listener 接口回调
*/
void button6Status(int status, FunctionListener listener);
}
MvpModelClass model 的实现类
package com.example.demo.mvp.model;
import com.example.demo.mvp.FunctionListener;
public class MvpModelClass implements MvpModelListener {
@Override
public void getButton1Status(int status, final FunctionListener listener) {
listener.success(1);
}
@Override
public void getButton2Status(int status, final FunctionListener listener) {
listener.success(2);
}
@Override
public void button3Status(int status, final FunctionListener listener) {
listener.success(3);
}
@Override
public void button4Status(int status, final FunctionListener listener) {
listener.success(4);
}
@Override
public void button5Status(int status, final FunctionListener listener) {
listener.success(5);
}
@Override
public void button6Status(int status, final FunctionListener listener) {
listener.success(6);
}
}
MvpPresenter Presenter接口
package com.example.demo.mvp.presenter;
public interface MvpPresenter {
/**
* 设置BUT1
*
* @param status 设置的状态
*/
void setButton1(int status);
/**
* 设置BUT2
*
* @param status 设置的状态
*/
void setButton2(int status);
/**
* 设置BUT3
*
* @param status 设置的状态
*/
void setButton3(int status);
/**
* 设置BUT4
*
* @param status 设置的状态
*/
void setButton4(int status);
/**
* 设置BUT5
*
* @param status 设置的状态
*/
void setButton5(int status);
/**
* 设置BUT6
*
* @param status 设置的状态
*/
void setButton6(int status);
/**
* 销毁
*/
void onDestroy();
}
MvpPresenterClass Presenter实现类
package com.example.demo.mvp.presenter;
import android.content.Context;
import com.example.demo.mvp.FunctionListener;
import com.example.demo.mvp.MvpView;
import com.example.demo.mvp.model.MvpModelClass;
import com.example.demo.mvp.model.MvpModelListener;
import java.util.ArrayList;
import java.util.List;
public class MvpPresenterClass implements MvpPresenter, FunctionListener {
private List<MvpView> mvpViewList = new ArrayList<>();
private final MvpModelListener mvpModel;
public MvpPresenterClass(Context context) {
this.mvpModel = new MvpModelClass();
}
public void registerCallback(MvpView mvpView) {
if (!mvpViewList.contains(mvpView)) {
mvpViewList.add(mvpView);
}
}
@Override
public void setButton1(int status) {
// 去获取数据
mvpModel.getButton1Status(1, this);
}
@Override
public void setButton2(int status) {
mvpModel.getButton2Status(1, this);
}
@Override
public void setButton3(int status) {
mvpModel.button3Status(1, this);
}
@Override
public void setButton4(int status) {
mvpModel.button4Status(1, this);
}
@Override
public void setButton5(int status) {
mvpModel.button5Status(1, this);
}
@Override
public void setButton6(int status) {
mvpModel.button6Status(1, this);
}
@Override
public void onDestroy() {
mvpViewList = null;
}
@Override
public void success(int state) {
switch (state) {
case 1:
for (int i = 0; i < mvpViewList.size(); i++) {
mvpViewList.get(i).bu1Status(state);
}
break;
case 2:
for (int i = 0; i < mvpViewList.size(); i++) {
mvpViewList.get(i).bu2Status(state);
}
break;
case 3:
for (int i = 0; i < mvpViewList.size(); i++) {
mvpViewList.get(i).bu3Status(state);
}
break;
case 4:
for (int i = 0; i < mvpViewList.size(); i++) {
mvpViewList.get(i).bu4Status(state);
}
break;
case 5:
for (int i = 0; i < mvpViewList.size(); i++) {
mvpViewList.get(i).bu5Status(state);
}
break;
case 6:
for (int i = 0; i < mvpViewList.size(); i++) {
mvpViewList.get(i).bu6Status(state);
}
break;
default:
break;
}
}
@Override
public void fail(Throwable throwable) {
}
}
MvpView mvp的view
package com.example.demo.mvp;
public interface MvpView {
/**
* 回调1按钮状态
*
* @param status 按钮状态
*/
void bu1Status(int status);
/**
* 回调2按钮状态
*
* @param status 按钮状态
*/
void bu2Status(int status);
/**
* 回调3按钮状态
*
* @param status 按钮状态
*/
void bu3Status(int status);
/**
* 回调4按钮状态
*
* @param status 按钮状态
*/
void bu4Status(int status);
/**
* 回调5按钮状态
*
* @param status 按钮状态
*/
void bu5Status(int status);
/**
* 回调6按钮状态
*
* @param status 按钮状态
*/
void bu6Status(int status);
}
FunctionListener
package com.example.demo.mvp;
public interface FunctionListener {
/**
* 回调成功状态
*
* @param o 回调数据
*/
void success(int state);
/**
* 回调失败状态
*
* @param throwable 失败
*/
void fail(Throwable throwable);
}
xml 布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/main_view"
layout="@layout/main_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<include
android:id="@+id/show_view"
layout="@layout/show_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<include
android:id="@+id/time_view"
layout="@layout/time_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/mvp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_1" />
<Button
android:id="@+id/mvp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_2" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/mvp3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_3" />
<Button
android:id="@+id/mvp4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_4" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/mvp5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_5" />
<Button
android:id="@+id/mvp6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_6" />
</LinearLayout>