创建 BaseFragment 被你的Fragment继承 onLazyLoad()方法 然后在onLazyLoad()方法中执行Fragment中的任务
public abstract class BaseFragment extends Fragment{
/**
* 懒加载过
*/
private boolean isLazyLoaded;
private boolean isPrepared;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
isPrepared = true;
//只有Fragment onCreateView好了,
//另外这里调用一次lazyLoad()
lazyLoad();
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
lazyLoad();
}
/**
* 调用懒加载
*/
private void lazyLoad() {
if (getUserVisibleHint() && isPrepared && !isLazyLoaded) {
onLazyLoad();
isLazyLoaded = true;
}
}
public abstract void onLazyLoad();
}