【Android】懒加载fragment+viewpager
2020-03-05 本文已影响0人
下雨天的小白鞋
BaseLoadFragment
当fragment可见的时候再加载数据
public abstract class BaseLoadFragment<P extends IPresenter> extends BaseFragment<P> {
private boolean isViewCreated; // 界面是否已创建完成
private boolean isVisibleToUser; // 是否对用户可见
private boolean isDataLoaded; // 数据是否已请求
/**
* 第一次可见时触发调用,此处实现具体的数据请求逻辑
*/
protected abstract void lazyLoadData();
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
this.isVisibleToUser = isVisibleToUser;
tryLoadData();
}
/**
* 保证在initData后触发
*/
@Override
public void onResume() {
super.onResume();
isViewCreated = true;
tryLoadData();
}
/**
* ViewPager场景下,判断父fragment是否可见
*/
private boolean isParentVisible() {
Fragment fragment = getParentFragment();
return fragment == null || (fragment instanceof BaseLoadFragment && ((BaseLoadFragment) fragment).isVisibleToUser);
}
/**
* ViewPager场景下,当前fragment可见时,如果其子fragment也可见,则让子fragment请求数据
*/
private void dispatchParentVisibleState() {
FragmentManager fragmentManager = getChildFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if (fragments.isEmpty()) {
return;
}
for (Fragment child : fragments) {
if (child instanceof BaseLoadFragment && ((BaseLoadFragment) child).isVisibleToUser) {
((BaseLoadFragment) child).tryLoadData();
}
}
}
public void tryLoadData() {
// boolean parentVisible = isParentVisible();
// if (isViewCreated && isVisibleToUser && parentVisible && !isDataLoaded) {
if (isViewCreated && isVisibleToUser && !isDataLoaded) {
lazyLoadData();
isDataLoaded = true;
//通知子Fragment请求数据
dispatchParentVisibleState();
}
}
}