很多时候 一个fragment可能放在不同地方,但是子类是做了某些刷新的封装,到第哪些需要懒加载哪些 不需要,或者需要但是 所嵌套的层次不同,都会出现不一样的毛病
比如子fragment,放在viewpager里面放在viewpager里面的 子fragment等等。如果出现问题了设置setOnStartNeedCheckData
为true即可。
package cn.qssq666.rapiddevelopframe.base.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.BuildConfig;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.qssq666.rapiddevelopframe.global.SuperAppContext;
import cn.qssq666.rapiddevelopframe.utils.FragmentUtil;
import cn.qssq666.rapiddevelopframe.utils.Prt;
/**
* Created by 情随事迁(qssq666@foxmail.com) on 2017/2/22.
*/
public abstract class BaseLazyLoadFragment extends SuperFragment implements FragmentUtil.FragmentOnStartNeedCheckVisible<BaseLazyLoadFragment> {
public final String TAG = this.getClass().getName();
protected boolean mIsViewCreated;
public View getView() {
return mView;
}
private View mView;
@Nullable
@Override
public final View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null || mIsViewCreated == false) {//mInitfalse则重新加载
mView = onCreateViewFix(inflater, container, savedInstanceState);
}
return mView;
}
abstract protected View onCreateViewFix(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);
@Override
public final void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
if (!mIsViewCreated) {
mIsViewCreated = true;
if (!isLazyLoadMode()) {
if(cn.qssq666.rapiddevelopframe.BuildConfig.DEBUG){
Log.w(TAG,"LAZY-FRAGMENT-INIT-onViewCreatedFix-ABSTRACT");
}
onViewCreatedFix(view, savedInstanceState);
} else {
lazyLoad();//但是如果不可见也是没法调用的.
}
}
}
public abstract void onViewCreatedFix(View view, @Nullable Bundle savedInstanceState);
@Override
public void onDestroyView() {
super.onDestroyView();
if (mView != null) {
ViewGroup viewGroup = ((ViewGroup) mView.getParent());
if (viewGroup != null) {
viewGroup.removeView(mView);
}
}
}
/**
* Fragment当前状态是否可见
*/
protected boolean isVisible = false;
protected boolean mIsLoadData;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getUserVisibleHint()) {
isVisible = true;
onVisible();
} else {
isVisible = false;
onInvisible();
}
}
/**
* 可见
*/
protected void onVisible() {
lazyLoad();
}
/**
* 不可见
*/
protected void onInvisible() {
}
@Override
public void onStart() {
super.onStart();
isVisible = true;
if (onStartNeedCheckData()) {
if (!hasInitData() && isLazyLoadMode()) {//在viewpager里面没问题,但是单纯的fragment如 畅销故事需要写这个.可能是因为adapter适配器的不同导致的.
lazyLoad();
}
}
}
public boolean onStartNeedCheckData() {
return mOnStartNeedCheckData;
}
/**
* 如果是fragment里面的fragment则需要。
*
* @param mOnStartNeedCheckData
* @return
*/
public BaseLazyLoadFragment setOnStartNeedCheckData(boolean mOnStartNeedCheckData) {
this.mOnStartNeedCheckData = mOnStartNeedCheckData;
return this;
}
boolean mOnStartNeedCheckData;
@Override
public void onStop() {
super.onStop();
isVisible = false;
}
public String getTAG() {
return "BaseLazy";
}
protected boolean hasInitData() {
return mIsLoadData;
}
/**
* 延迟加载
* 子类必须重写此方法
*/
protected void lazyLoad() {
if (isLazyLoadMode()) {
if (!mIsViewCreated || !isVisible) {
Log.w(TAG, "lazyLoad call return :init?" + mIsViewCreated + ",是否可见:" + isVisible);
return;
}
if (getActivity() == null) {
Prt.e(TAG, "activity为空" + this.getClass().getSimpleName());
if (BuildConfig.DEBUG) {
SuperAppContext.showToast("" + "activity为空" + this.getClass().getSimpleName());
}
return;
}
if (mIsLoadData) {
if(BuildConfig.DEBUG){
Log.w(TAG, "页面已经加载了并显示过了,忽略init " + this.getClass());
}
return;
}
mIsLoadData = true;
onViewCreatedFix(mView, null);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (BuildConfig.DEBUG) {
Prt.w(TAG, "onAttach");
}
}
protected boolean isLazyLoadMode() {
return lazyLoadMode;
}
public BaseLazyLoadFragment setLazyLoadMode(boolean lazyLoadMode) {
this.lazyLoadMode = lazyLoadMode;
return this;
}
boolean lazyLoadMode = true;
}