很久沒寫博客了,最近在忙點(diǎn)其他的事需纳。今天我來說下android中傳說的懶加載芦倒,網(wǎng)上好像有很多例子,大家也可以去搜下不翩。我這個(gè)demo包括了懶加載和懶加載中不同狀態(tài)布局的展示。下面看靜態(tài)效果圖(gif制作太煩人了)麻裳,所以要看動(dòng)畫效果的可直接下載apk查看口蝠。
效果圖:
loading.png
no data.png
error.png
實(shí)現(xiàn)過程就是viewpage+fragment左右滑動(dòng)。:
- BaseFragment中的主要方法講解
/**
* 加載成功后顯示的布局津坑,在此方法inflate布局,相當(dāng)于{@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
*/
@Nullable
public abstract View onCreateViewSuccess(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);
/**
* 當(dāng)fragment第一次可見的時(shí)候會(huì)回調(diào)此方法,可進(jìn)行findViewById或一些初始化操作
*/
public abstract void onInit(@Nullable Bundle savedInstanceState);
/**
* 可重寫此方法<br>
* 1妙蔗、可在此方法加載網(wǎng)絡(luò)或數(shù)據(jù)庫等數(shù)據(jù),如果是加載網(wǎng)絡(luò)請求(在子線程操作的)疆瑰,這里的返回值意義不大,因?yàn)檎埱筮€沒完已經(jīng)返回了<br>
* 2眉反、當(dāng)fragment第一次可見的時(shí)候會(huì)回調(diào)此方法昙啄,{@link #onInit(Bundle)}執(zhí)行完就會(huì)執(zhí)行此方法<br>
* 3、如果是網(wǎng)絡(luò)加載寸五,這里有可能會(huì)回調(diào)多次,回調(diào)多次的原因是看你設(shè)置的{@link LoadState}
*
* @param onLoadCallBack 可通過此參數(shù)控制界面展示梳凛。比如進(jìn)行網(wǎng)絡(luò)請求成功、失敗梳杏、數(shù)據(jù)空韧拒,都可通過此對象對界面的展示進(jìn)行控制<br>
* 設(shè)置方法為:<br>
* {@link LoadState.OnLoadCallBack#onEmpty()}<br>
* {@link LoadState.OnLoadCallBack#onError()} <br>
* {@link LoadState.OnLoadCallBack#onSuccess()}<br>
* {@link LoadState.OnLoadCallBack#onLoading()}
* @return {@link LoadState} <br>
* 1、如果fragment可見的時(shí)候界面需要立馬顯示(比如個(gè)人信息界面)并跳過網(wǎng)絡(luò)檢查步驟的十性,重寫此方法的返回值填這個(gè):{@link LoadState#STATE_SUCCESS}<br>
* 2叛溢、這里默認(rèn)會(huì)檢查網(wǎng)絡(luò)是否有連接<br>
* @see #setLoadState(int)
*/
public int onLoad(LoadState.OnLoadCallBack onLoadCallBack) {
if (!NetWorkUtil.isLinkedNetwork(getContext())) {
return LoadState.STATE_ERROR;
}
return LoadState.STATE_SUCCESS;
}
- Fragment中的如何實(shí)現(xiàn)懶加載
package com.awen.lazy.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.awen.lazy.BaseFragment;
import com.awen.lazy.R;
import com.awen.lazy.view.LoadState;
/**
* Created by Awen <Awentljs@gmail.com>
*/
public class FA extends BaseFragment {
@Nullable
@Override
public View onCreateViewSuccess(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment,container,false);
}
@Override
public void onInit(@Nullable Bundle savedInstanceState) {
TextView content = (TextView) getView().findViewById(R.id.content);
content.setText("A");
}
@Override
public int onLoad(final LoadState.OnLoadCallBack onLoadCallBack) {
//你可以在這里去加載網(wǎng)絡(luò)數(shù)據(jù)或數(shù)據(jù)庫數(shù)據(jù)
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(4000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//更新ui
onLoadCallBack.onSuccess();
//這樣設(shè)置也可以
//setLoadState(LoadState.STATE_SUCCESS);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
return super.onLoad(onLoadCallBack);
}
}
package com.awen.lazy.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.awen.lazy.BaseFragment;
import com.awen.lazy.R;
import com.awen.lazy.view.LoadState;
/**
* Created by Awen <Awentljs@gmail.com>
*/
public class FD extends BaseFragment {
@Nullable
@Override
public View onCreateViewSuccess(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment,container,false);
}
@Override
public void onInit(@Nullable Bundle savedInstanceState) {
TextView content = (TextView) getView().findViewById(R.id.content);
content.setText("D");
}
private boolean hasData;
@Override
public int onLoad(final LoadState.OnLoadCallBack onLoadCallBack) {
//你可以在這里去加載網(wǎng)絡(luò)數(shù)據(jù)或數(shù)據(jù)庫數(shù)據(jù)
new Thread(new Runnable() {
@Override
public void run() {
try {
//這里模擬網(wǎng)絡(luò)請求數(shù)據(jù):一開始數(shù)據(jù)為空,再點(diǎn)擊刷新劲适,有數(shù)據(jù)了
if(!hasData){
Thread.sleep(2000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
onLoadCallBack.onEmpty();
hasData = true;
}
});
}else {
Thread.sleep(2000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
onLoadCallBack.onSuccess();
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
return LoadState.STATE_LOADING;
}
}
實(shí)現(xiàn)懶加載主要重寫三個(gè)方法:onCreateViewSuccess()楷掉、onInit()、onLoad();
項(xiàng)目我已經(jīng)掛載在github,代碼量不多霞势,可下載看下烹植。還有代碼中的講解已經(jīng)很詳細(xì)了,如有不懂或疑問的可留言給我支示。