懒加载解决ViewPager中加载Fragment的问题
/**
* 懒加载
*
* @author -lw
* @date -2018/8/10
* @note -
* ---------------------------------------------------------------------------------------------------------------------
* @modified -
* @date -
* @note -
*/
public abstract class LazyFragment extends Fragment {
/** 是否视图已经初始化了 */
boolean mIsCreated;
/** 是否第一次加载 */
boolean mIsFirstLoad = true;
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (!hidden) {
lazyLoad(mIsFirstLoad);
mIsFirstLoad = false;
}
}
@Override
public void onResume() {
super.onResume();
if (getUserVisibleHint() && !isHidden()) {
lazyLoad(mIsFirstLoad);
mIsFirstLoad = false;
}
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mIsCreated = true;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && mIsCreated) {
lazyLoad(mIsFirstLoad);
mIsFirstLoad = false;
}
}
/**
* 是否是第一次加载
*
* @param isFirstLoad 是否是第一次加载
*/
public abstract void lazyLoad(boolean isFirstLoad);
}
网友评论