好处:可以快速地创建一套Fragment,view、presenter、model的关系自动绑定,使代码简洁美观。
只需继承Base类,无需重写任何方法
先给出用例(是不是超简洁):
//
//Fragment
//
public class HomeFragment extends BaseFragment<HomePresenter> {
@BindView(R2.id.home_test)
TextView tv_test;
@Override
protected int getResource() {
return R.layout.fragment_home;
}
@Override
protected void initView() {
tv_test.setText(requirePresenter().test());
}
}
//
//Presenter
//
public class HomePresenter extends BasePresenter<HomeFragment, HomeModel> {
public String test() {
return requireModel().test();
}
}
//
//Model
//
public class HomeModel extends BaseModel<HomePresenter> {
public String test() {
return "Test";
}
}
BaseFragment:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import java.lang.reflect.ParameterizedType;
import java.util.Objects;
import butterknife.ButterKnife;
@SuppressWarnings("rawtypes")
public abstract class BaseFragment<P extends BasePresenter> extends Fragment {
private P presenter;
private View root;
@SuppressWarnings("unchecked")
public BaseFragment() {
super();
try {
presenter = (P) ((Class<P>) ((ParameterizedType) (Objects.requireNonNull(getClass().getGenericSuperclass()))).getActualTypeArguments()[0]).newInstance();
presenter.setFragment(this);
} catch (IllegalAccessException | java.lang.InstantiationException e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(getResource(), container, false);
ButterKnife.bind(this, root);
initView();
return root;
}
protected P requirePresenter() {
return presenter;
}
protected View requireRoot() {
return root;
}
protected abstract int getResource();
protected abstract void initView();
}
BasePresenter:
import android.app.Activity;
import android.content.Context;
import java.lang.reflect.ParameterizedType;
import java.util.Objects;
@SuppressWarnings("rawtypes")
public abstract class BasePresenter<F extends BaseFragment, M extends BaseModel> {
private F fragment;
private M model;
void setFragment(F fragment) {
this.fragment = fragment;
}
@SuppressWarnings("unchecked")
public BasePresenter() {
try {
model = ((Class<M>) ((ParameterizedType) (Objects.requireNonNull(getClass().getGenericSuperclass()))).getActualTypeArguments()[1]).newInstance();
model.setPresenter(this);
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
}
protected Activity requireActivity() {
return fragment.requireActivity();
}
protected Context requireContext() {
return fragment.requireContext();
}
protected F requireFragment() {
return fragment;
}
protected M requireModel() {
return model;
}
}
BaseModel:
package us.xingkong.app.ui.base.fragment;
import androidx.lifecycle.ViewModel;
@SuppressWarnings("rawtypes")
public class BaseModel<P extends BasePresenter> extends ViewModel {
private P presenter;
void setPresenter(P presenter) {
this.presenter = presenter;
}
protected P requirePresenter() {
return presenter;
}
}