MVP 结构 --- 小demo展示理解 ```
作者:互联网
项目结构展示
BaseActivity
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity {
// T 泛型的本质是参数化类型,可以隐式和自动转为任意类型(数据类型)的参数
protected T mPresenter;
private Bundle mSavedInstanceState;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doBeforeSetContentView();
setContentView(getLayoutId());
//mvp
mPresenter = getPresenter();
if(mPresenter != null){
mPresenter.attachView(this);
}
/*EventBus 事件总线 注册
if(isRegisterEventBus()){
EventBusUtil.register(this);
}*/
mSavedInstanceState = savedInstanceState;
initData();
initView();
doAfterInit();
}
/**
* 初始化数据和视图后在 OnCreate 方法中的操作
*/
protected abstract void doAfterInit();
/**
* 初始化视图
*/
protected abstract void initView();
/**
* 初始化数据
*/
protected abstract void initData();
/**
* 是否注册EventBus,注册后才可以订阅事件
*
* @return 若需要注册 EventBus,则返回 true;否则返回 false
*/
protected abstract boolean isRegisterEventBus();
/**
* 获取当前活动的Presenter
*
* @return 相应的Presenter实例,没有则返回 null
*/
protected abstract T getPresenter();
/**
* 获取当前活动的布局
*
* @return 布局id
*/
protected abstract int getLayoutId();
/**
* 在setContentView之前被调用
*/
protected abstract void doBeforeSetContentView();
/**
* 弹出Toast
*
* @param content 弹出的内容
*/
protected void showShortToast(String content) {
//ToastUtil.showToast(this, content);
}
/**
* 跳转到另一活动
*
* @param activity 新活动.class
*/
protected void jumpToNewActivity(Class activity) {
Intent intent = new Intent(this, activity);
startActivity(intent);
}
/**
* 带Bundle的跳转活动
*
* @param activity 新活动.class
* @param bundle
*/
protected void jump2ActivityWithBundle(Class activity, Bundle bundle) {
Intent intent = new Intent(this, activity);
startActivity(intent, bundle);
}
/**
* 获取 onCreate 方法中的 Bundle 参数 savedInstanceState
*
* @return
*/
protected Bundle getSavedInstanceState() {
return mSavedInstanceState;
}
}
BaseFragment
public abstract class BaseFragment<V extends BasePresenter> extends Fragment {
protected V mPresenter; //该Fragment对应的Presenter
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = getPresenter();
if (mPresenter != null) {
mPresenter.attachView(this);
}
if (isRegisterEventBus()) {
//EventBusUtil.register(this);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
initView();
doInOnCreate();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(getLayoutId(), container, false); //第三个参数一定要设为false;
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.detachView();
}
if (isRegisterEventBus()) {
//EventBusUtil.unregister(this);
}
}
/**
* 获取fragment布局
*
* @return 布局id
*/
protected abstract int getLayoutId();
/**
* 在进行初始化布局前初始化相关数据
*/
protected abstract void initData();
/**
* 初始化视图
*/
protected abstract void initView();
/**
* 在onCreate方法中执行的操作
*/
protected abstract void doInOnCreate();
/**
* 获取Presenter实例
*
* @return 相应的Presenter实例,没有则返回 null
*/
protected abstract V getPresenter();
/**
* 是否注册EventBus,注册后才可以订阅事件
*
* @return 若需要注册 EventBus,则返回 true;否则返回 false
*/
protected abstract boolean isRegisterEventBus();
/**
* 显示 Toast
*
* @param content
*/
protected void showShortToast(String content) {
// ToastUtil.showToast(getContext(), content);
}
/**
* 跳转到活动
*
* @param activity 新活动.class
*/
protected void jump2Activity(Class activity) {
startActivity(new Intent(getContext(), activity));
}
/**
* 带Bundle的跳转活动
*
* @param activity 新活动.class
* @param bundle
*/
protected void jump2ActivityWithBundle(Class activity, Bundle bundle) {
Intent intent = new Intent(getContext(), activity);
startActivity(intent, bundle);
}
}
IReadContract
public interface IReadContract {
interface View{
void loadTxtSuccess(String text);
void loadTxtError(String errorMsg);
}
//Presenter 负责model 和view 的交互
interface Presenter{
void loadTxtSuccess(String text);
void loadTxtError(String errorMsg);
void loadTxt(String filePath);
}
interface Model{
void loadTxt(String filePath);
}
}
BasePresenter
public class BasePresenter<V> {
private V view;
// 添加布局
public void attachView(V view){
this.view = view;
}
//释放布局
public void detachView(){
this.view = null;
}
protected boolean isAttachView(){
return view != null;
}
protected V getMvpView(){
return view;
}
}
ReadPresenter
public class ReadPresenter extends BasePresenter<IReadContract.View> implements IReadContract.Presenter {
private IReadContract.Model mModel;
public ReadPresenter() {
mModel = new ReadModel(this);
}
//model获取数据后,View上数据的改变
@Override
public void loadTxtSuccess(String text) {
if (isAttachView()) {
getMvpView().loadTxtSuccess(text);//拿到当前视图id,可以操作控件的属性
//Log.d("tww","ReadPresenter-->getMvpView()");
}
}
@Override
public void loadTxtError(String errorMsg) {
}
//获取数据
@Override
public void loadTxt(String filePath) {
mModel.loadTxt(filePath);
}
}
ReadModel
public class ReadModel implements IReadContract.Model {
private IReadContract.Presenter mPresenter;
public ReadModel(IReadContract.Presenter mPresenter) {
this.mPresenter = mPresenter;
}
@Override
public void loadTxt(String filePath) {
//处理数据
Log.d("tww","ReadModel-->"+filePath);
mPresenter.loadTxtSuccess("ReadModel-->mPresenter.loadTxtSuccess");
}
}
ReadActivity
public class ReadActivity extends BaseActivity<ReadPresenter> implements IReadContract.View {
private Button mButton;
@Override
protected void doAfterInit() {
}
@Override
protected void initView() {
mButton = findViewById(R.id.bili_vp);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPresenter.loadTxt("loading...");
}
});
}
@Override
protected void initData() {
}
@Override
protected boolean isRegisterEventBus() {
return false;
}
@Override
protected ReadPresenter getPresenter() {
return new ReadPresenter();
}
@Override
protected int getLayoutId() {
return R.layout.activity_read;
}
@Override
protected void doBeforeSetContentView() {
}
@Override
public void loadTxtSuccess(String text) {
Log.d("tww","ReadActivity:"+text);
}
@Override
public void loadTxtError(String errorMsg) {
}
}
标签:MVP,demo,void,abstract,---,protected,Override,mPresenter,public 来源: https://www.cnblogs.com/kato-T/p/15967234.html