<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--正常時佈局-->
<include layout="@layout/activity_content"/>
<!--加載loading佈局-->
<include layout="@layout/activity_loading"/>
<!--異常時佈局-->
<include layout="@layout/activity_error"/>
<!--空數據時佈局-->
<include layout="@layout/activity_emptydata"/>
</LinearLayout>
複製代碼
public class LoadingView extends LinearLayout implements View.OnClickListener {
public static final int LOADING = 0;
public static final int STOP_LOADING = 1;
public static final int NO_DATA = 2;
public static final int NO_NETWORK = 3;
public static final int GONE = 4;
public static final int LOADING_DIALOG = 5;
private TextView mNoDataTextView;
private ProgressBar mLoadingProgressBar;
private RelativeLayout mRlError;
private LinearLayout mLlLoading;
private View mView;
private OnRefreshListener mListener;
public void setRefrechListener(OnRefreshListener mListener) {
this.mListener = mListener;
}
public interface OnRefreshListener {
void refresh();
}
public LoadingView(Context context) {
super(context);
init(context);
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.common_loading_get, this);
mLoadingProgressBar = (ProgressBar) mView.findViewById(R.id.mLoadingProgressBar);
mNoDataTextView = (TextView) mView.findViewById(R.id.mNoDataTextView);
mLlLoading = (LinearLayout) mView.findViewById(R.id.ll_loading);
mRlError = (RelativeLayout) mView.findViewById(R.id.rl_error);
mRlError.setOnClickListener(this);
setStatue(GONE);
}
public void setStatue(int status) {
setVisibility(View.VISIBLE);
try {
if (status == LOADING) {//更新
mRlError.setVisibility(View.GONE);
mLlLoading.setVisibility(View.VISIBLE);
} else if (status == STOP_LOADING) {
setVisibility(View.GONE);
} else if (status == NO_DATA) {//無數據狀況
mRlError.setVisibility(View.VISIBLE);
mLlLoading.setVisibility(View.GONE);
mNoDataTextView.setText("暫無數據");
} else if (status == NO_NETWORK) {//無網絡狀況
mRlError.setVisibility(View.VISIBLE);
mLlLoading.setVisibility(View.GONE);
mNoDataTextView.setText("網絡加載失敗,點擊從新加載");
} else {
setVisibility(View.GONE);
}
} catch (OutOfMemoryError e) {
}
}
@Override
public void onClick(View v) {
mListener.refresh();
setStatue(LOADING);
}
}
複製代碼
void showLoading(); //調用此方法顯示加載中的動畫
void showLoadFailed(); //調用此方法顯示加載失敗界面
void showEmpty(); //調用此方法顯示空頁面
void onClickRetry(); //子類中實現,點擊重試的回調方法
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.cheoo.app.view.recyclerview.TypeRecyclerView
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none">
</com.cheoo.app.view.recyclerview.TypeRecyclerView>
<com.cheoo.app.view.LoadingView
android:id="@+id/mLoadingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
複製代碼
mLoadingView = (LoadingView)findViewById(R.id.mLoadingView);
mLoadingView.setStatue(LoadingView.LOADING);
mLoadingView.setStatue(LoadingView.STOP_LOADING);
mLoadingView.setStatue(LoadingView.NO_NETWORK);
mLoadingView.setStatue(LoadingView.NO_DATA);
複製代碼
/**
* <pre>
* @author yangchong
* blog : https://github.com/yangchong211/YCStateLayout
* time : 2017/7/6
* desc : 自定義幀佈局
* revise:
* </pre>
*/
public class StateFrameLayout extends FrameLayout {
/**
* loading 加載id
*/
public static final int LAYOUT_LOADING_ID = 1;
/**
* 內容id
*/
public static final int LAYOUT_CONTENT_ID = 2;
/**
* 異常id
*/
public static final int LAYOUT_ERROR_ID = 3;
/**
* 網絡異常id
*/
public static final int LAYOUT_NETWORK_ERROR_ID = 4;
/**
* 空數據id
*/
public static final int LAYOUT_EMPTY_DATA_ID = 5;
/**
* 存放佈局集合
*/
private SparseArray<View> layoutSparseArray = new SparseArray<>();
//private HashMap<Integer,View> map = new HashMap<>();
/**
* 佈局管理器
*/
private StateLayoutManager mStatusLayoutManager;
public StateFrameLayout(Context context) {
super(context);
}
public StateFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StateFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setStatusLayoutManager(StateLayoutManager statusLayoutManager) {
mStatusLayoutManager = statusLayoutManager;
//添加全部的佈局到幀佈局
addAllLayoutToRootLayout();
}
private void addAllLayoutToRootLayout() {
if (mStatusLayoutManager.contentLayoutResId != 0) {
addLayoutResId(mStatusLayoutManager.contentLayoutResId, StateFrameLayout.LAYOUT_CONTENT_ID);
}
if (mStatusLayoutManager.loadingLayoutResId != 0) {
addLayoutResId(mStatusLayoutManager.loadingLayoutResId, StateFrameLayout.LAYOUT_LOADING_ID);
}
if (mStatusLayoutManager.emptyDataVs != null) {
addView(mStatusLayoutManager.emptyDataVs);
}
if (mStatusLayoutManager.errorVs != null) {
addView(mStatusLayoutManager.errorVs);
}
if (mStatusLayoutManager.netWorkErrorVs != null) {
addView(mStatusLayoutManager.netWorkErrorVs);
}
}
private void addLayoutResId(@LayoutRes int layoutResId, int id) {
View resView = LayoutInflater.from(mStatusLayoutManager.context).inflate(layoutResId, null);
layoutSparseArray.put(id, resView);
addView(resView);
}
/**
* 顯示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_EMPTY_DATA_ID)) {
showHideViewById(LAYOUT_EMPTY_DATA_ID);
emptyDataViewAddData(iconImage, textTip);
}
}
/**
* 根據ID顯示隱藏佈局
* @param id 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);
}
}
}
}
}
/**
* 這個是處理ViewStub的邏輯,主要有網絡異常佈局,加載異常佈局,空數據佈局
* @param id 佈局id
* @return 布爾值
*/
private boolean inflateLayout(int id) {
boolean isShow = true;
//若是爲null,則直接返回false
if (layoutSparseArray.get(id) == null) {
return false;
}
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_EMPTY_DATA_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;
default:
break;
}
return isShow;
}
}
複製代碼
public final class StateLayoutManager {
final Context context;
final int netWorkErrorRetryViewId;
final int emptyDataRetryViewId;
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 ViewStub emptyDataVs;
final ViewStub netWorkErrorVs;
final ViewStub errorVs;
final AbsViewStubLayout errorLayout;
final AbsViewStubLayout emptyDataLayout;
private final StateFrameLayout rootFrameLayout;
final OnShowHideViewListener onShowHideViewListener;
final OnRetryListener onRetryListener;
public static Builder newBuilder(Context context) {
return new Builder(context);
}
private 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 StateFrameLayout(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 AbsViewStubLayout errorLayout;
private AbsViewStubLayout emptyDataLayout;
private OnShowHideViewListener onShowHideViewListener;
private OnRetryListener onRetryListener;
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(AbsViewStubLayout errorLayout) {
this.errorLayout = errorLayout;
this.errorVs = errorLayout.getLayoutVs();
return this;
}
public Builder emptyDataLayout(AbsViewStubLayout emptyDataLayout) {
this.emptyDataLayout = emptyDataLayout;
this.emptyDataVs = emptyDataLayout.getLayoutVs();
return this;
}
public Builder netWorkErrorRetryViewId(@LayoutRes int netWorkErrorRetryViewId) {
this.netWorkErrorRetryViewId = netWorkErrorRetryViewId;
return this;
}
public Builder emptyDataRetryViewId(@LayoutRes int emptyDataRetryViewId) {
this.emptyDataRetryViewId = emptyDataRetryViewId;
return this;
}
public Builder errorRetryViewId(@LayoutRes int errorRetryViewId) {
this.errorRetryViewId = errorRetryViewId;
return this;
}
public Builder retryViewId(@LayoutRes int retryViewId) {
this.retryViewId = retryViewId;
return this;
}
public Builder emptyDataIconImageId(@LayoutRes int emptyDataIconImageId) {
this.emptyDataIconImageId = emptyDataIconImageId;
return this;
}
public Builder emptyDataTextTipId(@LayoutRes int emptyDataTextTipId) {
this.emptyDataTextTipId = emptyDataTextTipId;
return this;
}
public Builder errorIconImageId(@LayoutRes int errorIconImageId) {
this.errorIconImageId = errorIconImageId;
return this;
}
public Builder errorTextTipId(@LayoutRes int errorTextTipId) {
this.errorTextTipId = errorTextTipId;
return this;
}
/**
* 爲狀態View顯示隱藏監聽事件
* @param listener listener
* @return
*/
public Builder onShowHideViewListener(OnShowHideViewListener listener) {
this.onShowHideViewListener = listener;
return this;
}
/**
* 爲重試加載按鈕的監聽事件
* @param onRetryListener listener
* @return
*/
public Builder onRetryListener(OnRetryListener onRetryListener) {
this.onRetryListener = onRetryListener;
return this;
}
/**
* 建立對象
* @return
*/
public StateLayoutManager build() {
return new StateLayoutManager(this);
}
}
}
複製代碼
/**存放佈局集合 */
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);
}
//那麼哪裏從集合中取數據呢
public void showContent() {
if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null) {
showHideViewById(LAYOUT_CONTENT_ID);
}
}
複製代碼
/**
* 顯示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);
}
//調用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;
}
複製代碼
/**
* 重試加載
*/
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();
}
});
}
複製代碼
/**
* ================================================
* 做 者:楊充
* 版 本: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();
}
}
複製代碼
@Override
protected void initStatusLayout() {
statusLayoutManager = StateLayoutManager.newBuilder(this)
.contentView(R.layout.activity_main)
.emptyDataView(R.layout.activity_emptydata)
.errorView(R.layout.activity_error)
.loadingView(R.layout.activity_loading)
.netWorkErrorView(R.layout.activity_networkerror)
.build();
}
//或者添加上監聽事件
@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();
}
//如何切換狀態呢?
showContent();
showEmptyData();
showError();
showLoading();
showNetWorkError();
//或者這樣操做也能夠
statusLayoutManager.showLoading();
statusLayoutManager.showContent();
複製代碼
/**
* 點擊從新刷新
*/
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();
}
});
}
複製代碼