@@ -0,0 +1,105 @@
|
||||
package com.mogo.commons.mvp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
/**
|
||||
* 基础Fragment
|
||||
*/
|
||||
public abstract class BaseFragment extends Fragment {
|
||||
private Context mContext;
|
||||
protected View mRootView;
|
||||
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
if (mRootView == null) {
|
||||
mRootView = inflater.inflate(getLayoutId(), container, false);
|
||||
} else {
|
||||
ViewGroup viewGroup = (ViewGroup) mRootView.getParent();
|
||||
if (viewGroup != null) {
|
||||
viewGroup.removeView(mRootView);
|
||||
}
|
||||
}
|
||||
return mRootView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
initViews();
|
||||
initViews(savedInstanceState);
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局资源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract int getLayoutId();
|
||||
|
||||
/**
|
||||
* 获取绑定的TAG标记
|
||||
*
|
||||
* @return 返回唯一TAG标记
|
||||
*/
|
||||
public abstract String getTagName();
|
||||
|
||||
/**
|
||||
* 初始化控件,必须在初始化完成之后才可以实例化presenter,避免
|
||||
* presenter 生命周期错乱
|
||||
*/
|
||||
protected abstract void initViews();
|
||||
|
||||
protected void initViews(Bundle savedInstanceState) {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected <T extends View> T findViewById(int id) {
|
||||
if (mRootView == null) {
|
||||
mRootView = getView();
|
||||
}
|
||||
if (mRootView != null) {
|
||||
return (T) mRootView.findViewById(id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Context getContext() {
|
||||
if (mContext == null) {
|
||||
mContext = super.getContext();
|
||||
}
|
||||
return mContext;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
mRootView = null;
|
||||
mContext = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +1,9 @@
|
||||
package com.mogo.commons.mvp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
@@ -18,68 +11,17 @@ import com.mogo.utils.logger.Logger;
|
||||
* <p>
|
||||
* mvp fragment
|
||||
*/
|
||||
public abstract class MvpFragment<V extends IView, P extends Presenter<V>> extends Fragment implements IView {
|
||||
public abstract class MvpFragment<V extends IView, P extends Presenter<V>> extends BaseFragment implements IView {
|
||||
|
||||
private Context mContext;
|
||||
protected P mPresenter;
|
||||
protected View mRootView;
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
if (mRootView == null) {
|
||||
mRootView = inflater.inflate(getLayoutId(), container, false);
|
||||
} else {
|
||||
ViewGroup viewGroup = (ViewGroup) mRootView.getParent();
|
||||
if (viewGroup != null) {
|
||||
viewGroup.removeView(mRootView);
|
||||
}
|
||||
}
|
||||
return mRootView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局资源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract int getLayoutId();
|
||||
|
||||
/**
|
||||
* 获取绑定的TAG标记
|
||||
* @return 返回唯一TAG标记
|
||||
*/
|
||||
public abstract String getTagName();
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
initViews();
|
||||
initViews(savedInstanceState);
|
||||
mPresenter = createPresenter();
|
||||
getViewLifecycleOwner().getLifecycle().addObserver(mPresenter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化控件,必须在初始化完成之后才可以实例化presenter,避免
|
||||
* presenter 生命周期错乱
|
||||
*/
|
||||
protected abstract void initViews();
|
||||
|
||||
protected void initViews(Bundle savedInstanceState) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 presenter 实例
|
||||
*
|
||||
@@ -92,26 +34,6 @@ public abstract class MvpFragment<V extends IView, P extends Presenter<V>> exten
|
||||
return mPresenter;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected <T extends View> T findViewById(int id) {
|
||||
if (mRootView == null) {
|
||||
mRootView = getView();
|
||||
}
|
||||
if (mRootView != null) {
|
||||
return (T) mRootView.findViewById(id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Context getContext() {
|
||||
if (mContext == null) {
|
||||
mContext = super.getContext();
|
||||
}
|
||||
return mContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
@@ -119,7 +41,5 @@ public abstract class MvpFragment<V extends IView, P extends Presenter<V>> exten
|
||||
getViewLifecycleOwner().getLifecycle().removeObserver(mPresenter);
|
||||
}
|
||||
mPresenter = null;
|
||||
mRootView = null;
|
||||
mContext = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user