不讲原理,不花里胡哨,直接说咋用
一般在做项目时 会碰见这种界面,上面有tab指示器下面可以滑动,一般的做法是tablayout+viewpager+fragment去实现,但是如果需要展示的tab很多的话,那么所有的fragment会直接加载完,这样的话不仅影响性能,而且也是耗流量的.如同uc浏览器的新闻分类一样,它是当你去滑倒这个tab的时候才去加载数据,这个时候就需要对fragment进行懒加载配置了.
/**
* @return The current value of the user-visible hint on this fragment.
* @see #setUserVisibleHint(boolean)
*/
public boolean getUserVisibleHint() {
return mUserVisibleHint;
}
这个方法会在当前界面用户可见的时候调用,执行顺序在createview之前.返回值mUserVisibleHint为true代表当前界面可见
定义两个参数
protected boolean mIsInitView = false;//是否初始化视图
protected boolean mIsVisible = false;//是否可见
然后再在方法中判断是否去加载数据
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
mIsVisible = true;
isCanLoadData();
} else {
mIsVisible = false;
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//复用mView
if (mView == null) {
mView = inflater.inflate(getLayoutId(), container, false);
}
initView(mView);
mIsInitView = true;
isCanLoadData();
return mView;
}
/**
*是否可以加载数据
*/
private void isCanLoadData() {
//所以条件是view初始化完成并且对用户可见
if (mIsInitView && mIsVisible) {
initData();
//防止重复加载数据
mIsInitView = false;
mIsVisible = false;
}
}
我自己的baselazy基类代码
public abstract class LazyLoadFragment<T extends IBasePresenter> extends Fragment
implements IBaseView, SwipeRefreshLayout.OnRefreshListener {
protected Context mContext;
protected Activity mActivity;
protected T mPresenter;
protected Unbinder unbinder;
protected View mView;
private AlertDialog mDialog;
protected boolean mIsInitView = false;
protected boolean mIsVisible = false;
/**
* 获取fragment的布局id
*
* @return
*/
protected abstract @LayoutRes
int getLayoutId();
/**
* 创建Presenter
*
* @return
*/
protected abstract T createPresenter();
/**
* view的一些初始化操作
*
* @param mView
*/
protected abstract void initView(View mView);
/**
* 数据的初始化,包括网络请求
*/
protected abstract void initData();
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
mIsVisible = true;
isCanLoadData();
} else {
mIsVisible = false;
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getContext();
mActivity = getActivity();
mPresenter = createPresenter();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//复用mView
if (mView == null) {
mView = inflater.inflate(getLayoutId(), container, false);
}
unbinder = ButterKnife.bind(this, mView);
initView(mView);
mIsInitView = true;
isCanLoadData();
return mView;
}
private void isCanLoadData() {
//所以条件是view初始化完成并且对用户可见
if (mIsInitView && mIsVisible) {
initData();
//防止重复加载数据
mIsInitView = false;
mIsVisible = false;
}
}
@Override
public void onDestroyView() {
if (mPresenter != null) {
mPresenter.dispose();
}
if (unbinder != null) {
unbinder.unbind();
}
super.onDestroyView();
}
@Override
public void onDestroy() {
if (mPresenter != null) {
mPresenter.detachView(); //解除presenter与view的绑定
}
super.onDestroy();
}
@Override
public Context provideContext() {
return mContext;
}
@Override
public void showLoading() {
if (mActivity instanceof BaseActivity) {
((BaseActivity) mActivity).showLoading();
}
}
@Override
public void dismissLoading() {
if (mActivity instanceof BaseActivity) {
((BaseActivity) mActivity).dismissLoading();
}
}
@Override
public void requestFail(String message) {
dismissLoading();
}
}