關(guān)于Android加載狀態(tài)視圖切換
目錄介紹
1.關(guān)于Android界面切換狀態(tài)的介紹
2.思路轉(zhuǎn)變,抽取分離類(lèi)管理幾種狀態(tài)
3.關(guān)于該狀態(tài)切換工具優(yōu)點(diǎn)分析
4.如何實(shí)現(xiàn)的步驟
5.使用方法介紹和Demo下載
好消息
- 博客筆記大匯總【16年3月到至今】,包括Java基礎(chǔ)及深入知識(shí)點(diǎn),Android技術(shù)博客,Python學(xué)習(xí)筆記等等,還包括平時(shí)開(kāi)發(fā)中遇到的bug匯總,當(dāng)然也在工作之余收集了大量的面試題,長(zhǎng)期更新維護(hù)并且修正,持續(xù)完善……開(kāi)源的文件是markdown格式的!同時(shí)也開(kāi)源了生活博客,從12年起,積累共計(jì)47篇[近20萬(wàn)字],轉(zhuǎn)載請(qǐng)注明出處,謝謝!
- 鏈接地址:https://github.com/yangchong211/YCBlogs
- 如果覺(jué)得好,可以star一下,謝謝!當(dāng)然也歡迎提出建議,萬(wàn)事起于忽微,量變引起質(zhì)變!
1.關(guān)于Android界面切換狀態(tài)的介紹
怎樣切換界面狀態(tài)?有些界面想定制自定義狀態(tài)?狀態(tài)如何添加點(diǎn)擊事件?下面就為解決這些問(wèn)題!
內(nèi)容界面
加載數(shù)據(jù)中
加載數(shù)據(jù)錯(cuò)誤
加載后沒(méi)有數(shù)據(jù)
沒(méi)有網(wǎng)絡(luò)
2.思路轉(zhuǎn)變,抽取分離類(lèi)管理幾種狀態(tài)
以前做法:
直接把這些界面include到main界面中,然后動(dòng)態(tài)去切換界面,后來(lái)發(fā)現(xiàn)這樣處理不容易復(fù)用到其他項(xiàng)目中,而且在activity中處理這些狀態(tài)的顯示和隱藏比較亂
利用子類(lèi)繼承父類(lèi)特性,在父類(lèi)中寫(xiě)切換狀態(tài),但有些界面如果沒(méi)有繼承父類(lèi),又該如何處理
現(xiàn)在做法:
讓View狀態(tài)的切換和Activity徹底分離開(kāi),必須把這些狀態(tài)View都封裝到一個(gè)管理類(lèi)中,然后暴露出幾個(gè)方法來(lái)實(shí)現(xiàn)View之間的切換。
在不同的項(xiàng)目中可以需要的View也不一樣,所以考慮把管理類(lèi)設(shè)計(jì)成builder模式來(lái)自由的添加需要的狀態(tài)View
3.關(guān)于該狀態(tài)切換工具優(yōu)點(diǎn)分析
可以自由切換內(nèi)容,空數(shù)據(jù),異常錯(cuò)誤,加載,網(wǎng)絡(luò)錯(cuò)誤等5種狀態(tài)
父類(lèi)BaseActivity直接暴露5中狀態(tài),方便子類(lèi)統(tǒng)一管理狀態(tài)切換
/**
* ================================================
* 作 者:楊充
* 版 本:1.0
* 創(chuàng)建日期:2017/7/6
* 描 述:抽取類(lèi)
* 修訂歷史:
* ================================================
*/
public abstract class BaseActivity extends AppCompatActivity {
protected StatusLayoutManager statusLayoutManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_view);
initStatusLayout();
initBaseView();
initToolBar();
initView();
}
protected abstract void initStatusLayout();
protected abstract void initView();
/**
* 獲取到布局
*/
private void initBaseView() {
LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);
ll_main.addView(statusLayoutManager.getRootLayout());
}
//正常展示數(shù)據(jù)狀態(tài)
protected void showContent() {
statusLayoutManager.showContent();
}
//加載數(shù)據(jù)為空時(shí)狀態(tài)
protected void showEmptyData() {
statusLayoutManager.showEmptyData();
}
//加載數(shù)據(jù)錯(cuò)誤時(shí)狀態(tài)
protected void showError() {
statusLayoutManager.showError();
}
//網(wǎng)絡(luò)錯(cuò)誤時(shí)狀態(tài)
protected void showNetWorkError() {
statusLayoutManager.showNetWorkError();
}
//正在加載中狀態(tài)
protected void showLoading() {
statusLayoutManager.showLoading();
}
}
當(dāng)狀態(tài)是加載數(shù)據(jù)失敗時(shí),點(diǎn)擊可以刷新數(shù)據(jù);當(dāng)狀態(tài)是無(wú)網(wǎng)絡(luò)時(shí),點(diǎn)擊可以設(shè)置網(wǎng)絡(luò)
/**
* 點(diǎn)擊重新刷新
*/
private void initErrorDataView() {
statusLayoutManager.showError();
LinearLayout ll_error_data = (LinearLayout) findViewById(R.id.ll_error_data);
ll_error_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
initData();
adapter.notifyDataSetChanged();
showContent();
}
});
}
/**
* 點(diǎn)擊設(shè)置網(wǎng)絡(luò)
*/
private void initSettingNetwork() {
statusLayoutManager.showNetWorkError();
LinearLayout ll_set_network = (LinearLayout) findViewById(R.id.ll_set_network);
ll_set_network.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
startActivity(intent);
}
});
}
倘若有些頁(yè)面想定制狀態(tài)布局,也可以自由實(shí)現(xiàn),很簡(jiǎn)單:
/**
* 自定義加載數(shù)據(jù)為空時(shí)的狀態(tài)布局
*/
private void initEmptyDataView() {
statusLayoutManager.showEmptyData();
//此處是自己定義的狀態(tài)布局
**statusLayoutManager.showLayoutEmptyData(R.layout.activity_emptydata);**
LinearLayout ll_empty_data = (LinearLayout) findViewById(R.id.ll_empty_data);
ll_empty_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
initData();
adapter.notifyDataSetChanged();
showContent();
}
});
}
4.如何實(shí)現(xiàn)的步驟
1.先看看狀態(tài)管理器類(lèi)【builder建造者模式】
loadingLayoutResId和contentLayoutResId代表等待加載和顯示內(nèi)容的xml文件
幾種異常狀態(tài)要用ViewStub,因?yàn)樵诮缑鏍顟B(tài)切換中l(wèi)oading和內(nèi)容View都是一直需要加載顯示的,但是其他的3個(gè)只有在沒(méi)數(shù)據(jù)或者網(wǎng)絡(luò)異常的情況下才會(huì)加載顯示,所以用ViewStub來(lái)加載他們可以提高性能。
public class StateLayoutManager {
final Context context;
final ViewStub netWorkErrorVs;
final int netWorkErrorRetryViewId;
final ViewStub emptyDataVs;
final int emptyDataRetryViewId;
final ViewStub errorVs;
final int errorRetryViewId;
final int loadingLayoutResId;
final int contentLayoutResId;
final int retryViewId;
final int emptyDataIconImageId;
final int emptyDataTextTipId;
final int errorIconImageId;
final int errorTextTipId;
final VLayout errorLayout;
final VLayout emptyDataLayout;
final RootFrameLayout rootFrameLayout;
final OnShowHideViewListener onShowHideViewListener;
final OnRetryListener onRetryListener;
public StateLayoutManager(Builder builder) {
this.context = builder.context;
this.loadingLayoutResId = builder.loadingLayoutResId;
this.netWorkErrorVs = builder.netWorkErrorVs;
this.netWorkErrorRetryViewId = builder.netWorkErrorRetryViewId;
this.emptyDataVs = builder.emptyDataVs;
this.emptyDataRetryViewId = builder.emptyDataRetryViewId;
this.errorVs = builder.errorVs;
this.errorRetryViewId = builder.errorRetryViewId;
this.contentLayoutResId = builder.contentLayoutResId;
this.onShowHideViewListener = builder.onShowHideViewListener;
this.retryViewId = builder.retryViewId;
this.onRetryListener = builder.onRetryListener;
this.emptyDataIconImageId = builder.emptyDataIconImageId;
this.emptyDataTextTipId = builder.emptyDataTextTipId;
this.errorIconImageId = builder.errorIconImageId;
this.errorTextTipId = builder.errorTextTipId;
this.errorLayout = builder.errorLayout;
this.emptyDataLayout = builder.emptyDataLayout;
rootFrameLayout = new RootFrameLayout(this.context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
rootFrameLayout.setLayoutParams(layoutParams);
rootFrameLayout.setStatusLayoutManager(this);
}
/**
* 顯示loading
*/
public void showLoading() {
rootFrameLayout.showLoading();
}
/**
* 顯示內(nèi)容
*/
public void showContent() {
rootFrameLayout.showContent();
}
/**
* 顯示空數(shù)據(jù)
*/
public void showEmptyData(int iconImage, String textTip) {
rootFrameLayout.showEmptyData(iconImage, textTip);
}
/**
* 顯示空數(shù)據(jù)
*/
public void showEmptyData() {
showEmptyData(0, "");
}
/**
* 顯示空數(shù)據(jù)
*/
public void showLayoutEmptyData(Object... objects) {
rootFrameLayout.showLayoutEmptyData(objects);
}
/**
* 顯示網(wǎng)絡(luò)異常
*/
public void showNetWorkError() {
rootFrameLayout.showNetWorkError();
}
/**
* 顯示異常
*/
public void showError(int iconImage, String textTip) {
rootFrameLayout.showError(iconImage, textTip);
}
/**
* 顯示異常
*/
public void showError() {
showError(0, "");
}
public void showLayoutError(Object... objects) {
rootFrameLayout.showLayoutError(objects);
}
/**
* 得到root 布局
*/
public View getRootLayout() {
return rootFrameLayout;
}
public static final class Builder {
private Context context;
private int loadingLayoutResId;
private int contentLayoutResId;
private ViewStub netWorkErrorVs;
private int netWorkErrorRetryViewId;
private ViewStub emptyDataVs;
private int emptyDataRetryViewId;
private ViewStub errorVs;
private int errorRetryViewId;
private int retryViewId;
private int emptyDataIconImageId;
private int emptyDataTextTipId;
private int errorIconImageId;
private int errorTextTipId;
private VLayout errorLayout;
private VLayout emptyDataLayout;
private OnShowHideViewListener onShowHideViewListener;
private OnRetryListener onRetryListener;
public Builder(Context context) {
this.context = context;
}
/**
* 自定義加載布局
*/
public Builder loadingView(@LayoutRes int loadingLayoutResId) {
this.loadingLayoutResId = loadingLayoutResId;
return this;
}
/**
* 自定義網(wǎng)絡(luò)錯(cuò)誤布局
*/
public Builder netWorkErrorView(@LayoutRes int newWorkErrorId) {
netWorkErrorVs = new ViewStub(context);
netWorkErrorVs.setLayoutResource(newWorkErrorId);
return this;
}
/**
* 自定義加載空數(shù)據(jù)布局
*/
public Builder emptyDataView(@LayoutRes int noDataViewId) {
emptyDataVs = new ViewStub(context);
emptyDataVs.setLayoutResource(noDataViewId);
return this;
}
/**
* 自定義加載錯(cuò)誤布局
*/
public Builder errorView(@LayoutRes int errorViewId) {
errorVs = new ViewStub(context);
errorVs.setLayoutResource(errorViewId);
return this;
}
/**
* 自定義加載內(nèi)容正常布局
*/
public Builder contentView(@LayoutRes int contentLayoutResId) {
this.contentLayoutResId = contentLayoutResId;
return this;
}
public Builder errorLayout(VLayout errorLayout) {
this.errorLayout = errorLayout;
this.errorVs = errorLayout.getLayoutVs();
return this;
}
public Builder emptyDataLayout(VLayout emptyDataLayout) {
this.emptyDataLayout = emptyDataLayout;
this.emptyDataVs = emptyDataLayout.getLayoutVs();
return this;
}
public Builder netWorkErrorRetryViewId(int netWorkErrorRetryViewId) {
this.netWorkErrorRetryViewId = netWorkErrorRetryViewId;
return this;
}
public Builder emptyDataRetryViewId(int emptyDataRetryViewId) {
this.emptyDataRetryViewId = emptyDataRetryViewId;
return this;
}
public Builder errorRetryViewId(int errorRetryViewId) {
this.errorRetryViewId = errorRetryViewId;
return this;
}
public Builder retryViewId(int retryViewId) {
this.retryViewId = retryViewId;
return this;
}
public Builder emptyDataIconImageId(int emptyDataIconImageId) {
this.emptyDataIconImageId = emptyDataIconImageId;
return this;
}
public Builder emptyDataTextTipId(int emptyDataTextTipId) {
this.emptyDataTextTipId = emptyDataTextTipId;
return this;
}
public Builder errorIconImageId(int errorIconImageId) {
this.errorIconImageId = errorIconImageId;
return this;
}
public Builder errorTextTipId(int errorTextTipId) {
this.errorTextTipId = errorTextTipId;
return this;
}
public Builder onShowHideViewListener(OnShowHideViewListener onShowHideViewListener) {
this.onShowHideViewListener = onShowHideViewListener;
return this;
}
public Builder onRetryListener(OnRetryListener onRetryListener) {
this.onRetryListener = onRetryListener;
return this;
}
public StateLayoutManager build() {
return new StateLayoutManager(this);
}
}
public static Builder newBuilder(Context context) {
return new Builder(context);
}
}
2.大約5種狀態(tài),如何管理這些狀態(tài)?添加到集合中,Android中選用SparseArray比HashMap更省內(nèi)存,在某些條件下性能更好,主要是因?yàn)樗苊饬藢?duì)key的自動(dòng)裝箱(int轉(zhuǎn)為Integer類(lèi)型),它內(nèi)部則是通過(guò)兩個(gè)數(shù)組來(lái)進(jìn)行數(shù)據(jù)存儲(chǔ)的,一個(gè)存儲(chǔ)key,另外一個(gè)存儲(chǔ)value,為了優(yōu)化性能,它內(nèi)部對(duì)數(shù)據(jù)還采取了壓縮的方式來(lái)表示稀疏數(shù)組的數(shù)據(jù),從而節(jié)約內(nèi)存空間
/**存放布局集合 */
private SparseArray<View> layoutSparseArray = new SparseArray();
/**將布局添加到集合 */
……
private void addLayoutResId(@LayoutRes int layoutResId, int id) {
View resView = LayoutInflater.from(mStatusLayoutManager.context).inflate(layoutResId, null);
**layoutSparseArray.put(id, resView);**
addView(resView);
}
3.當(dāng)顯示某個(gè)布局時(shí),調(diào)用的方法如下
方法里面通過(guò)id判斷來(lái)執(zhí)行不同的代碼,首先判斷ViewStub是否為空,如果為空就代表沒(méi)有添加這個(gè)View就返回false,不為空就加載View并且添加到集合當(dāng)中,然后調(diào)用showHideViewById方法顯示隱藏View,retryLoad方法是給重試按鈕添加事件
/**
* 顯示loading
*/
public void showLoading() {
if (layoutSparseArray.get(LAYOUT_LOADING_ID) != null)
**showHideViewById**(LAYOUT_LOADING_ID);
}
/**
* 顯示內(nèi)容
*/
public void showContent() {
if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null)
**showHideViewById**(LAYOUT_CONTENT_ID);
}
/**
* 顯示空數(shù)據(jù)
*/
public void showEmptyData(int iconImage, String textTip) {
if (**inflateLayout**(LAYOUT_EMPTYDATA_ID)) {
showHideViewById(LAYOUT_EMPTYDATA_ID);
emptyDataViewAddData(iconImage, textTip);
}
}
/**
* 顯示網(wǎng)絡(luò)異常
*/
public void showNetWorkError() {
if (**inflateLayout**(LAYOUT_NETWORK_ERROR_ID))
showHideViewById(LAYOUT_NETWORK_ERROR_ID);
}
/**
* 顯示異常
*/
public void showError(int iconImage, String textTip) {
if (**inflateLayout**(LAYOUT_ERROR_ID)) {
showHideViewById(LAYOUT_ERROR_ID);
errorViewAddData(iconImage, textTip);
}
}
//調(diào)用inflateLayout方法,方法返回true然后調(diào)用showHideViewById方法
private boolean inflateLayout(int id) {
boolean isShow = true;
if (layoutSparseArray.get(id) != null) return isShow;
switch (id) {
case LAYOUT_NETWORK_ERROR_ID:
if (mStatusLayoutManager.netWorkErrorVs != null) {
View view = mStatusLayoutManager.netWorkErrorVs.inflate();
retryLoad(view, mStatusLayoutManager.netWorkErrorRetryViewId);
layoutSparseArray.put(id, view);
isShow = true;
} else {
isShow = false;
}
break;
case LAYOUT_ERROR_ID:
if (mStatusLayoutManager.errorVs != null) {
View view = mStatusLayoutManager.errorVs.inflate();
if (mStatusLayoutManager.errorLayout != null) mStatusLayoutManager.errorLayout.setView(view);
retryLoad(view, mStatusLayoutManager.errorRetryViewId);
layoutSparseArray.put(id, view);
isShow = true;
} else {
isShow = false;
}
break;
case LAYOUT_EMPTYDATA_ID:
if (mStatusLayoutManager.emptyDataVs != null) {
View view = mStatusLayoutManager.emptyDataVs.inflate();
if (mStatusLayoutManager.emptyDataLayout != null) mStatusLayoutManager.emptyDataLayout.setView(view);
retryLoad(view, mStatusLayoutManager.emptyDataRetryViewId);
layoutSparseArray.put(id, view);
isShow = true;
} else {
isShow = false;
}
break;
}
return isShow;
}
4.然后在根據(jù)id隱藏布局
通過(guò)id找到需要顯示的View并且顯示它,隱藏其他View,如果顯示隱藏監(jiān)聽(tīng)事件不為空,就分別調(diào)用它的顯示和隱藏方法
/**
* 根據(jù)ID顯示隱藏布局
* @param id
*/
private void showHideViewById(int id) {
for (int i = 0; i < layoutSparseArray.size(); i++) {
int key = layoutSparseArray.keyAt(i);
View valueView = layoutSparseArray.valueAt(i);
//顯示該view
if(key == id) {
valueView.setVisibility(View.VISIBLE);
if(mStatusLayoutManager.onShowHideViewListener != null) mStatusLayoutManager.onShowHideViewListener.onShowView(valueView, key);
} else {
if(valueView.getVisibility() != View.GONE) {
valueView.setVisibility(View.GONE);
if(mStatusLayoutManager.onShowHideViewListener != null) mStatusLayoutManager.onShowHideViewListener.onHideView(valueView, key);
}
}
}
}
5.最后看看重新加載方法
/**
* 重試加載
*/
private void retryLoad(View view, int id) {
View retryView = view.findViewById(mStatusLayoutManager.retryViewId != 0 ? mStatusLayoutManager.retryViewId : id);
if (retryView == null || mStatusLayoutManager.onRetryListener == null) return;
retryView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mStatusLayoutManager.onRetryListener.onRetry();
}
});
}
5.使用方法介紹
1.直接在Activity中添加代碼
@Override
protected void initStatusLayout() {
statusLayoutManager = StateLayoutManager.newBuilder(this)
.contentView(R.layout.activity_content_data)
.emptyDataView(R.layout.activity_empty_data)
.errorView(R.layout.activity_error_data)
.loadingView(R.layout.activity_loading_data)
.netWorkErrorView(R.layout.activity_networkerror)
.onRetryListener(new OnRetryListener() {
@Override
public void onRetry() {
//為重試加載按鈕的監(jiān)聽(tīng)事件
}
})
.onShowHideViewListener(new OnShowHideViewListener() {
@Override
public void onShowView(View view, int id) {
//為狀態(tài)View顯示監(jiān)聽(tīng)事件
}
@Override
public void onHideView(View view, int id) {
//為狀態(tài)View隱藏監(jiān)聽(tīng)事件
}
})
.build();
}
2.在父類(lèi)中重寫(xiě)以下幾個(gè)方法,子類(lèi)直接繼承就行
//正常展示數(shù)據(jù)狀態(tài)
protected void showContent() {
statusLayoutManager.showContent();
}
//加載數(shù)據(jù)為空時(shí)狀態(tài)
protected void showEmptyData() {
statusLayoutManager.showEmptyData();
}
//加載數(shù)據(jù)錯(cuò)誤時(shí)狀態(tài)
protected void showError() {
statusLayoutManager.showError();
}
//網(wǎng)絡(luò)錯(cuò)誤時(shí)狀態(tài)
protected void showNetWorkError() {
statusLayoutManager.showNetWorkError();
}
//正在加載中狀態(tài)
protected void showLoading() {
statusLayoutManager.showLoading();
}
3.更加詳細(xì)的介紹,可以直接參考Demo
https://github.com/yangchong211/YCStateLayout