一般写Fragment继承android.support.v4.app.Fragment 重写Fragment的生命周期实现创建过程:添加布局
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = null;
if (v == null) {
v = inflater.inflate(R.layout.common_booklist_fragment, container, false);
ButterKnife.bind(this, v);
emptyLayout = new EmptyLayout(getActivity(), listview);
}
return v;
}
BaseLazyFragment.java
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
int layoutId = getContentViewLayoutID();
if (layoutId != 0) {
return inflater.inflate(layoutId, container, false);
} else {
return super.onCreateView(inflater, container, savedInstanceState);
}
}
protected abstract int getContentViewLayoutID();//获取布局xml
使用的话很简单继承BaseLazyFragment重写getContentViewLayoutID()就行。
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
initViewsAndEvents(view);
}
protected abstract void initViewsAndEvents(View view);//初始化控件,如果使用butterknife就不需要写findViewById;
第一次可见状态、可见状态、第一次不可见状态、不可见状态
private boolean isFirstVisible = true;
private boolean isFirstInvisible = true;
private boolean isPrepared;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initPrepare();
}
private synchronized void initPrepare() {
if (isPrepared) {
onFirstUserVisible();
} else {
isPrepared = true;
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
if (isFirstVisible) {
isFirstVisible = false;
initPrepare();
} else {
onUserVisible();
}
} else {
if (isFirstInvisible) {
isFirstInvisible = false;
onFirstUserInvisible();
} else {
onUserInvisible();
}
}
}
protected abstract void onFirstUserVisible();
protected abstract void onUserVisible();
private void onFirstUserInvisible() { }
protected abstract void onUserInvisible();
/**
* 封装跳转
*/
protected void readyGo(Class> clazz) {
Intent intent = new Intent(getActivity(), clazz);
startActivity(intent);
}
protected void readyGo(Class> clazz, Bundle bundle) {
Intent intent = new Intent(getActivity(), clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
}
protected void readyGoForResult(Class> clazz, int requestCode) {
Intent intent = new Intent(getActivity(), clazz);
startActivityForResult(intent, requestCode);
}
protected void readyGoForResult(Class> clazz, int requestCode, Bundle bundle) {
Intent intent = new Intent(getActivity(), clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
/**
* Created by ziyang on 16/12/28.
* Version 1.0
* 封装Fragment懒加载
*
* onCreateView() xml
* onViewCreated() butterKnife initView()
*/
public abstract class BaseLazyFragment extends Fragment {
private boolean isPrepared;
private boolean isFirstVisible = true;
private boolean isFirstInvisible = true;
private Dialog mPro;
protected MyToast myToast;
protected Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == DIALOG_SHOW) {
mPro.show();
} else {
mPro.dismiss();
}
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
int layoutId = getContentViewLayoutID();
if (layoutId != 0) {
return inflater.inflate(layoutId, container, false);
} else {
return super.onCreateView(inflater, container, savedInstanceState);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initPrepare();
mPro = createLoadingDialog1(getActivity());
myToast = new MyToast(getActivity());
}
private synchronized void initPrepare() {
if (isPrepared) {
onFirstUserVisible();
} else {
isPrepared = true;
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
initViewsAndEvents(view);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
if (isFirstVisible) {
isFirstVisible = false;
initPrepare();
} else {
onUserVisible();
}
} else {
if (isFirstInvisible) {
isFirstInvisible = false;
onFirstUserInvisible();
} else {
onUserInvisible();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
destroyViewAndThing();
}
protected abstract void onFirstUserVisible();//加载数据,开启动画/广播..
protected abstract void onUserVisible();///开启动画/广播..
private void onFirstUserInvisible() {
}
protected abstract void onUserInvisible();//暂停动画,暂停广播
protected abstract int getContentViewLayoutID();
protected abstract void initViewsAndEvents(View view);
protected abstract void destroyViewAndThing();//销毁动作
public Dialog createLoadingDialog1(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.loading_dialog, null); // 得到加载view
RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.rl_probar); // 加载布局
Dialog loadingDialog = new Dialog(context, R.style.loading_dialog); // 创建自定义样式dialog
layout.setVisibility(View.VISIBLE);
loadingDialog.setCancelable(true);// 不可以用"返回键"取消
loadingDialog.setCanceledOnTouchOutside(true);
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
return loadingDialog;
}
/**
* 封装跳转
*/
protected void readyGo(Class> clazz) {
Intent intent = new Intent(getActivity(), clazz);
startActivity(intent);
}
protected void readyGo(Class> clazz, Bundle bundle) {
Intent intent = new Intent(getActivity(), clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
}
protected void readyGoForResult(Class> clazz, int requestCode) {
Intent intent = new Intent(getActivity(), clazz);
startActivityForResult(intent, requestCode);
}
protected void readyGoForResult(Class> clazz, int requestCode, Bundle bundle) {
Intent intent = new Intent(getActivity(), clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
}
当然还有MVP的封装:
public abstract class BaseFragment extends SupportFragment {
private final String TAG = getClass().getSimpleName();
protected T mPresenter;
protected Context mContext;
protected View rootView;
protected Unbinder unbinder;
private boolean isViewPrepared; // 标识fragment视图已经初始化完毕
private boolean hasFetchData; // 标识已经触发过懒加载数据
@Override
public void onAttach(Context mContext) {
super.onAttach(mContext);
KL.d(this.getClass(), getName() + "------>onAttach");
if (mContext != null) {
this.mContext = mContext;
} else {
this.mContext = getActivity();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
KL.d(this.getClass(), getName() + "------>onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
KL.d(this.getClass(), getName() + "------>onCreateView");
if (rootView == null) {
rootView = inflater.inflate(getLayout(), container, false);
}
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
}
unbinder = ButterKnife.bind(this, rootView);
initView(inflater);
EventBus.getDefault().register(this);
setTitleHeight(rootView);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
KL.d(this.getClass(), getName() + "------>onActivityCreated");
initEvent();
}
@Override
public void onStart() {
super.onStart();
KL.d(this.getClass(), getName() + "------>onStart");
}
@Override
public void onResume() {
super.onResume();
KL.d(this.getClass(), getName() + "------>onResume");
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
KL.d(this.getClass(), getName() + "------>onViewCreated");
isViewPrepared = true;
lazyFetchDataIfPrepared();
}
@Override
public void onPause() {
super.onPause();
KL.d(this.getClass(), getName() + "------>onPause");
}
@Override
public void onStop() {
super.onStop();
KL.d(this.getClass(), getName() + "------>onStop");
}
@Override
public void onDestroyView() {
EventBus.getDefault().unregister(this);
super.onDestroyView();
KL.d(this.getClass(), getName() + "------>onDestroyView");
// view被销毁后,将可以重新触发数据懒加载,因为在viewpager下,fragment不会再次新建并走onCreate的生命周期流程,将从onCreateView开始
hasFetchData = false;
isViewPrepared = false;
mPresenter = null;
}
@Override
public void onDestroy() {
super.onDestroy();
KL.d(this.getClass(), getName() + "------>onDestroy");
if (unbinder != null)
unbinder.unbind();
}
@Override
public void onDetach() {
super.onDetach();
KL.d(this.getClass(), getName() + "------>onDetach");
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
Log.v(TAG, getClass().getName() + "------>isVisibleToUser = " + isVisibleToUser);
if (isVisibleToUser) {
lazyFetchDataIfPrepared();
}
}
private void lazyFetchDataIfPrepared() {
// 用户可见fragment && 没有加载过数据 && 视图已经准备完毕
if (getUserVisibleHint() && !hasFetchData && isViewPrepared) {
hasFetchData = true;
lazyFetchData();
}
}
/**
* 懒加载的方式获取数据,仅在满足fragment可见和视图已经准备好的时候调用一次
*/
protected void lazyFetchData() {
Log.v(TAG, getClass().getName() + "------>lazyFetchData");
}
public String getName() {
return BaseFragment.class.getName();
}
protected abstract int getLayout();
protected void initView(LayoutInflater inflater) {
}
protected void initEvent() {
}
}
参考链接:Android谈谈封装那些事–BaseActivity和BaseFragment(二)
封装篇——Fragment懒加载