- 抽象活动基类,抽取公共方法
public abstract class AbstractActivity extends AppCompatActivity {
// 解决 rxjava 请求内存泄漏问题
protected CompositeDisposable mCompositeDisposable;
protected ImageView backImg;
protected TextView title;
protected LoadingPage loading;
/**
* 获取布局id
*
* @return 布局id
*/
protected abstract int getLayout();
/**
* 初始化布局
*/
protected abstract void initView();
/**
* 初始化点击事件监听器
*/
protected abstract void initListener();
/**
* 初始化数据
*/
protected abstract void initData();
protected void initDisposable() {
if (mCompositeDisposable == null) {
mCompositeDisposable = new CompositeDisposable();
}
}
public CompositeDisposable getCompositeDisposable() {
initDisposable();
return mCompositeDisposable;
}
public void setTitle(String titleTxt) {
if (title != null && titleTxt != null) {
title.setText(titleTxt);
}
}
public void setTitle(int titleId) {
if (title != null) {
title.setText(titleId);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
AppManager.getAppManager().finishActivity();
}
//------------------------------隐藏键盘----------------------------
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (this == null) {
LogUtil.e("dispatchTouchEvent", "context == null");
return false;
}
// 自动隐藏键盘
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager)
this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否则所有的组件都不会有TouchEvent了
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
private boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = {0, 0};
// 获取输入框当前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
float x = event.getX();
float y = event.getY();
if (x > left && x < right && y > top && y < bottom) {
// 点击的是输入框区域,保留点击EditText的事件
return false;
} else {
return true;
}
}
// 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点
return false;
}
//此方法只是关闭软键盘
protected void hintKbTwo() {
if (this == null) return;
InputMethodManager imm = (InputMethodManager)this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()&&getCurrentFocus()!=null){
if (getCurrentFocus().getWindowToken()!=null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
protected Context getMyContext() {
return this;
}
}
- BaseMvpActivity
public abstract class BaseMvpActivity<P extends IPresenter<V>, V extends IView> extends AbstractActivity {
protected P mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
AppManager.getAppManager(this).addActivity(this);
initView();
mPresenter = initPreseter();
initDisposable();
attach();
initListener();
initData();
}
@Override
protected void onResume() {
initDisposable();
super.onResume();
attach();
}
private void attach() {
if (mPresenter != null) {
mPresenter.attach((V)this);
}
}
protected abstract P initPreseter();
@Override
protected void onDestroy() {
if(mPresenter != null) {
mPresenter.dettach();
}
super.onDestroy();
}
}
- BaseMvpLazyFragment
/**
* 抽取fragment 的 mvp 模式基类, 使用mvp是为了让 view 层跟 model 层充分解耦,便于维护与单元测试
* 懒加载
* * @param <P> presenter
* @param <V> view
*/
public abstract class BaseMvpLazyFragment<P extends IPresenter<V>, V extends IView> extends AbstractFragment {
protected boolean isFirst;
protected P mPresenter;
/**
* 初始化presenter
*/
protected abstract P initPreseter();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(getLayout(), container, false);
}
initView();
initPreseter();
attach();
initListener();
LogUtil.w(TAG, "onCreateView()");
return mView;
}
@Override
public void onResume() {
super.onResume();
LogUtil.w(TAG, "onResume() 是否第一次加载 " + !isFirst);
attach();
// 是否首次加载该页面
if(!isFirst) {
isFirst = true;
initData();
}
}
/**
* 绑定view
*/
private void attach() {
if (mPresenter != null) {
mPresenter.attach((V)this);
}
}
@Override
public void onDetach() {
if(mPresenter != null) {
mPresenter.dettach();
}
isFirst = false;
super.onDetach();
LogUtil.w(TAG, "onDetach()");
}
}
-
二:IView + IPresenter + 响应体泛型 + presenter 弱引用持有
https://www.jianshu.com/p/22e38f6aac40 -
推荐
Android Retrofit2.0使用详解
https://blog.csdn.net/qq_35229022/article/details/93849660
网友评论