工作中需要用到懶加載的地方可能有很多,比如ViewPager+Fragment的使用時,要求不同的Fragment的狀態欄顏色不一樣,這樣就需要動態修改狀態欄的顏色。
因爲viewpager是有最小緩存的,所以直接在fragment裏寫修改每個fragment的狀態欄顏色是不會起作用的。因爲緩存的作用,代碼已經提前執行了。所以必須用Fragment懶加載,當gragment可見時去修改狀態欄的顏色,這樣只是修改當前的Fragment的,不會影響其他fragment。
先說一下,修改狀態欄顏色
網上有很多解決方法,Github上也有不少開源庫,比如StatusBarUtil和ImmersionBar,自己選擇其中一種方案即可
Fragment懶加載的實現
先貼代碼
參考博客ViewPager+Fragment 取消 (禁止) 預加載延遲加載 (懶加載) 問題解決方案
package com.yunduo.wisdom.base;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yunduo.wisdom.util.eventbus.EventBusUtil;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
/**
* Created by parade歲月 on 2019/8/16 0:04
*/
public abstract class BaseFragment extends Fragment {
private View mRootView;
protected Context mContext;
private Unbinder unbinder;
//視圖是否已經完成初始化
private boolean isViewCreated;
//是否已經預加載過數據
private boolean isLoad;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (mContext != null){
mContext = context;
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null){
mRootView = inflater.inflate(getLayoutId(), container, false);
}
unbinder = ButterKnife.bind(this, mRootView);
if (isRegisteredEventBus()){
EventBusUtil.register(this);
}
initViews();
initDatas();
setEvents();
return mRootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
isViewCreated = true;
isCanLoadData();
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isCanLoadData();
}
private void isCanLoadData() {
if (!isViewCreated){
return;
}
if (getUserVisibleHint()){
lazyLoad();
isLoad = true;
}
}
/**
* 設置佈局
* @return fragment的佈局資源
*/
protected abstract int getLayoutId();
/**
* 此方法內進行佈局綁定、View初始化等操作
*/
protected abstract void initViews();
/**
*在佈局加載後執行(有可能佈局還不可見),建議在此方法內加載數據和處理佈局顯示數據
*/
protected abstract void initDatas();
/**
* 預加載時用這個方法
*/
protected abstract void lazyLoad();
/**
*建議在此方法內綁定設置監聽器、設置執行回調事件等操作
*/
protected abstract void setEvents();
public final <T extends View> T findViewById(@IdRes int id) {
return mRootView.findViewById(id);
}
public void getDataFromNet(Observable observable, Observer observer){
observable.observeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
/**
* 是否註冊事件分發 默認不註冊
* 重寫此方法返回true來註冊EventBus
* @return true:註冊;false:不註冊
*/
protected boolean isRegisteredEventBus(){
return false;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
isViewCreated = false;
isLoad = false;
if (isRegisteredEventBus()){
EventBusUtil.unregister(this);
}
}
@Override
public void onResume() {
super.onResume();
if (isRegisteredEventBus()){
if (!EventBusUtil.isRegistered(this)){
EventBusUtil.register(this);
}
}
}
}
修改fragment的狀態欄的背景色
在fragment的lazyLoad()
方法裏利用前面提到的工具修改狀態欄顏色即可