場(chǎng)景:
- 第一次進(jìn)入界面
- 左右滑動(dòng)界面
setUserVisibleHint()被調(diào)用是在onCreateView()之前,基于這個(gè)特點(diǎn):
- 在setUserVisibleHint()中初始化 isVisibleToUser = true卷要,表示可見了独榴,這時(shí)候調(diào)用prepareFetchData()并不會(huì)被執(zhí)行棺榔,因?yàn)閛nActivityCreated()沒初始化
- 在onActivityCreated()中初始化 isViewInitiated = true隘道,表示生命周期被調(diào)用,這時(shí)候調(diào)用prepareFetchData()會(huì)執(zhí)行忘晤。
- 實(shí)現(xiàn)懶加載的效果,需要數(shù)據(jù)只初始化一次设塔,所以在 prepareFetchData() 再設(shè)置一個(gè)變量 isDataInitiated = true 表示數(shù)據(jù)已經(jīng)初始化過闰蛔。
- 這樣頁(yè)面切換后每次都會(huì)調(diào)用 setUserVisibleHint(),如果數(shù)據(jù)初始化過 isDataInitiated = true钞护, 數(shù)據(jù)就不會(huì)被再次加載
總結(jié):每次滑動(dòng)界面都會(huì)調(diào)用setUserVisibleHint() ,在其中加載數(shù)據(jù)难咕。但是執(zhí)行這個(gè)方法需要確保 onActivityCreated() 初始化過,數(shù)據(jù)也沒有加載過
public abstract class LazyLoadFragmen extends BaseFragment {
protected boolean isViewInitiated;
protected boolean isVisibleToUser;
protected boolean isDataInitiated;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("Fragment生命周期", "onActivityCreated");
isViewInitiated = true;
prepareFetchData();
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
Log.e("Fragment生命周期", "setUserVisibleHint");
this.isVisibleToUser = isVisibleToUser;
prepareFetchData();
}
public abstract void fetchData();
public boolean prepareFetchData() {
return prepareFetchData(false);
}
public boolean prepareFetchData(boolean forceUpdate) {
if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
fetchData();
isDataInitiated = true;
return true;
}
return false;
}
}
或
/**
* Created by zkt on 2018-8-17.
* Description:
*/
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
/**
* 懶加載
*/
public abstract class LazyLoadFragment extends Fragment {
// 標(biāo)志位,標(biāo)志已經(jīng)初始化完成跨算,因?yàn)閟etUserVisibleHint是在onCreateView之前調(diào)?的,
// 在視圖未初始化的時(shí)候步势,在lazyLoad當(dāng)中就使?的話背犯,就會(huì)有空指針的異常
private boolean isFragmentPrepared;
// 標(biāo)志當(dāng)前??是否可?
private boolean isVisible;
// 標(biāo)志數(shù)據(jù)是否加載過
private boolean isDataPrepared;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
//懶加載
if (getUserVisibleHint()) {
isVisible = true;
onVisible();
} else {
isVisible = false;
onInvisible();
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
isFragmentPrepared = true;
lazyLoad();
}
protected void onVisible() {
lazyLoad();
}
protected void onInvisible() {
}
//可見 + Fragment加載過 + 數(shù)據(jù)沒加載過
protected void lazyLoad() {
if (isVisible && isFragmentPrepared && !isDataPrepared) {
isDataPrepared = true;
getData();
}
}
/* protected void lazyLoad() {
if (!isVisible || !isPrepared) {
return;
}
//數(shù)據(jù)請(qǐng)求
getData();
}
*/
public abstract void getData();
}