關于Android加載狀態視圖切換
目錄介紹
1.關于Android界面切換狀態的介紹
2.思路轉變,抽取分離類管理幾種狀態
3.關于該狀態切換工具優點分析
4.如何實現的步驟
5.使用方法介紹和Demo下載
好消息
- 博客筆記大匯總【16年3月到至今】,包括Java基礎及深入知識點,Android技術博客,Python學習筆記等等,還包括平時開發中遇到的bug匯總,當然也在工作之余收集了大量的面試題,長期更新維護并且修正,持續完善……開源的文件是markdown格式的!同時也開源了生活博客,從12年起,積累共計47篇[近20萬字],轉載請注明出處,謝謝!
- 鏈接地址:https://github.com/yangchong211/YCBlogs
- 如果覺得好,可以star一下,謝謝!當然也歡迎提出建議,萬事起于忽微,量變引起質變!
1.關于Android界面切換狀態的介紹
怎樣切換界面狀態?有些界面想定制自定義狀態?狀態如何添加點擊事件?下面就為解決這些問題!
內容界面
加載數據中
加載數據錯誤
加載后沒有數據
沒有網絡
2.思路轉變,抽取分離類管理幾種狀態
以前做法:
直接把這些界面include到main界面中,然后動態去切換界面,后來發現這樣處理不容易復用到其他項目中,而且在activity中處理這些狀態的顯示和隱藏比較亂
利用子類繼承父類特性,在父類中寫切換狀態,但有些界面如果沒有繼承父類,又該如何處理
現在做法:
讓View狀態的切換和Activity徹底分離開,必須把這些狀態View都封裝到一個管理類中,然后暴露出幾個方法來實現View之間的切換。
在不同的項目中可以需要的View也不一樣,所以考慮把管理類設計成builder模式來自由的添加需要的狀態View
3.關于該狀態切換工具優點分析
可以自由切換內容,空數據,異常錯誤,加載,網絡錯誤等5種狀態
父類BaseActivity直接暴露5中狀態,方便子類統一管理狀態切換
/**
* ================================================
* 作 者:楊充
* 版 本:1.0
* 創建日期:2017/7/6
* 描 述:抽取類
* 修訂歷史:
* ================================================
*/
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());
}
//正常展示數據狀態
protected void showContent() {
statusLayoutManager.showContent();
}
//加載數據為空時狀態
protected void showEmptyData() {
statusLayoutManager.showEmptyData();
}
//加載數據錯誤時狀態
protected void showError() {
statusLayoutManager.showError();
}
//網絡錯誤時狀態
protected void showNetWorkError() {
statusLayoutManager.showNetWorkError();
}
//正在加載中狀態
protected void showLoading() {
statusLayoutManager.showLoading();
}
}
當狀態是加載數據失敗時,點擊可以刷新數據;當狀態是無網絡時,點擊可以設置網絡
/**
* 點擊重新刷新
*/
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();
}
});
}
/**
* 點擊設置網絡
*/
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);
}
});
}
倘若有些頁面想定制狀態布局,也可以自由實現,很簡單:
/**
* 自定義加載數據為空時的狀態布局
*/
private void initEmptyDataView() {
statusLayoutManager.showEmptyData();
//此處是自己定義的狀態布局
**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.如何實現的步驟
1.先看看狀態管理器類【builder建造者模式】
loadingLayoutResId和contentLayoutResId代表等待加載和顯示內容的xml文件
幾種異常狀態要用ViewStub,因為在界面狀態切換中loading和內容View都是一直需要加載顯示的,但是其他的3個只有在沒數據或者網絡異常的情況下才會加載顯示,所以用ViewStub來加載他們可以提高性能。
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();
}
/**
* 顯示內容
*/
public void showContent() {
rootFrameLayout.showContent();
}
/**
* 顯示空數據
*/
public void showEmptyData(int iconImage, String textTip) {
rootFrameLayout.showEmptyData(iconImage, textTip);
}
/**
* 顯示空數據
*/
public void showEmptyData() {
showEmptyData(0, "");
}
/**
* 顯示空數據
*/
public void showLayoutEmptyData(Object... objects) {
rootFrameLayout.showLayoutEmptyData(objects);
}
/**
* 顯示網絡異常
*/
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;
}
/**
* 自定義網絡錯誤布局
*/
public Builder netWorkErrorView(@LayoutRes int newWorkErrorId) {
netWorkErrorVs = new ViewStub(context);
netWorkErrorVs.setLayoutResource(newWorkErrorId);
return this;
}
/**
* 自定義加載空數據布局
*/
public Builder emptyDataView(@LayoutRes int noDataViewId) {
emptyDataVs = new ViewStub(context);
emptyDataVs.setLayoutResource(noDataViewId);
return this;
}
/**
* 自定義加載錯誤布局
*/
public Builder errorView(@LayoutRes int errorViewId) {
errorVs = new ViewStub(context);
errorVs.setLayoutResource(errorViewId);
return this;
}
/**
* 自定義加載內容正常布局
*/
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種狀態,如何管理這些狀態?添加到集合中,Android中選用SparseArray比HashMap更省內存,在某些條件下性能更好,主要是因為它避免了對key的自動裝箱(int轉為Integer類型),它內部則是通過兩個數組來進行數據存儲的,一個存儲key,另外一個存儲value,為了優化性能,它內部對數據還采取了壓縮的方式來表示稀疏數組的數據,從而節約內存空間
/**存放布局集合 */
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.當顯示某個布局時,調用的方法如下
方法里面通過id判斷來執行不同的代碼,首先判斷ViewStub是否為空,如果為空就代表沒有添加這個View就返回false,不為空就加載View并且添加到集合當中,然后調用showHideViewById方法顯示隱藏View,retryLoad方法是給重試按鈕添加事件
/**
* 顯示loading
*/
public void showLoading() {
if (layoutSparseArray.get(LAYOUT_LOADING_ID) != null)
**showHideViewById**(LAYOUT_LOADING_ID);
}
/**
* 顯示內容
*/
public void showContent() {
if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null)
**showHideViewById**(LAYOUT_CONTENT_ID);
}
/**
* 顯示空數據
*/
public void showEmptyData(int iconImage, String textTip) {
if (**inflateLayout**(LAYOUT_EMPTYDATA_ID)) {
showHideViewById(LAYOUT_EMPTYDATA_ID);
emptyDataViewAddData(iconImage, textTip);
}
}
/**
* 顯示網絡異常
*/
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);
}
}
//調用inflateLayout方法,方法返回true然后調用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.然后在根據id隱藏布局
通過id找到需要顯示的View并且顯示它,隱藏其他View,如果顯示隱藏監聽事件不為空,就分別調用它的顯示和隱藏方法
/**
* 根據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() {
//為重試加載按鈕的監聽事件
}
})
.onShowHideViewListener(new OnShowHideViewListener() {
@Override
public void onShowView(View view, int id) {
//為狀態View顯示監聽事件
}
@Override
public void onHideView(View view, int id) {
//為狀態View隱藏監聽事件
}
})
.build();
}
2.在父類中重寫以下幾個方法,子類直接繼承就行
//正常展示數據狀態
protected void showContent() {
statusLayoutManager.showContent();
}
//加載數據為空時狀態
protected void showEmptyData() {
statusLayoutManager.showEmptyData();
}
//加載數據錯誤時狀態
protected void showError() {
statusLayoutManager.showError();
}
//網絡錯誤時狀態
protected void showNetWorkError() {
statusLayoutManager.showNetWorkError();
}
//正在加載中狀態
protected void showLoading() {
statusLayoutManager.showLoading();
}
3.更加詳細的介紹,可以直接參考Demo
https://github.com/yangchong211/YCStateLayout