MultipleStatusView项目地址
概述
- 该自定义控件继承自RelativeLayout
- 内容页展示与其他状态页展示逻辑不同,将围绕这两个来说.
内容页展示逻辑
- 当XML加载完成立即展示内容页
@Override
protected void onFinishInflate() {
super.onFinishInflate();
showContent();
}
- 手动调用展示内容页
@Override public void onClick(View v) {
switch (v.getId()) {
case R.id.fab_content:
mMultipleStatusView.showContent();
break;
}
}
- 内容页展示逻辑
public final void showContent() {
changeViewStatus(STATUS_CONTENT);
if (null == mContentView && mContentViewResId != NULL_RESOURCE_ID) {
mContentView = mInflater.inflate(mContentViewResId, null);
addView(mContentView, 0, DEFAULT_LAYOUT_PARAMS);
}
showContentView();
}
private void showContentView() {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
view.setVisibility(mOtherIds.contains(view.getId()) ? View.GONE : View.VISIBLE);
}
}
private void changeViewStatus(int newViewStatus) {
if (mViewStatus == newViewStatus) {
return;
}
if (null != mViewStatusListener) {
mViewStatusListener.onChange(mViewStatus, newViewStatus);
}
mViewStatus = newViewStatus;
}
其他状态页展示
showEmpty(int hintResId, Object... formatArgs)
public final void showEmpty(int hintResId, Object... formatArgs) {
showEmpty();
setStatusHintContent(mEmptyView, hintResId, formatArgs);
}
private void setStatusHintContent(View view, int resId, Object... formatArgs) {
checkNull(view, "Target view is null.");
setStatusHintContent(view, view.getContext().getString(resId, formatArgs));
}
private void setStatusHintContent(View view, String hint) {
checkNull(view, "Target view is null.");
TextView hintView = view.findViewById(R.id.status_hint_content);
if (null != hintView) {
hintView.setText(hint);
} else {
throw new NullPointerException("Not find the view ID `status_hint_content`");
}
}
showEmpty()
public final void showEmpty() {
showEmpty(mEmptyViewResId, DEFAULT_LAYOUT_PARAMS);
}
public final void showEmpty(int layoutId, ViewGroup.LayoutParams layoutParams) {
showEmpty(null == mEmptyView ? inflateView(layoutId) : mEmptyView, layoutParams);
}
public final void showEmpty(View view, ViewGroup.LayoutParams layoutParams) {
checkNull(view, "Empty view is null.");
checkNull(layoutParams, "Layout params is null.");
changeViewStatus(STATUS_EMPTY);
if (null == mEmptyView) {
mEmptyView = view;
View emptyRetryView = mEmptyView.findViewById(R.id.empty_retry_view);
if (null != mOnRetryClickListener && null != emptyRetryView) {
emptyRetryView.setOnClickListener(mOnRetryClickListener);
}
mOtherIds.add(mEmptyView.getId());
addView(mEmptyView, 0, layoutParams);
}
showViewById(mEmptyView.getId());
}
private void showViewById(int viewId) {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
view.setVisibility(view.getId() == viewId ? View.VISIBLE : View.GONE);
}
}
使用
<com.classic.common.MultipleStatusView
android:id="@+id/SimpleMultiStateView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:emptyView="@layout/view_empty"
app:errorView="@layout/view_err"
app:loadingView="@layout/view_loading"
app:noNetworkView="@layout/view_nonet">
<im.delight.android.webview.AdvancedWebView
android:id="@+id/awv_help"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none" />
</com.classic.common.MultipleStatusView>
<!--@layout/view_nonet:view_nonet代表layout布局文件,当网络超时,该布局会显示在屏幕最顶层被用户看到 -->
- 当使用自定义状态布局得时候,状态布局得父控件必须按照MultipleStatusView 控件得规则来写。
- 加载中视图父容器id必须为:
loading_view
- 空视图父容器id必须为:
empty_view
- 错误视图父容器id必须为:
error_view
- 无网络视图父容器id必须为:
no_network_view
- 当需要为状态视图中得某控件设置点击事件得时候,被点击的控件的ID命名规则也应该按照MultipleStatusView 控件的规则来。
- 空视图内被点击view id:
empty_retry_view
- 错误视图内被点击view id:
error_retry_view
- 无网络视图内被点击view id:
no_network_retry_view
- 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/no_network_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/refresh_bg">
<ImageView
android:id="@+id/no_network_retry_view"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="350dp"
android:src="@mipmap/refresh_btn" />
</RelativeLayout>
MultipleStatusView multipleStatusView = (MultipleStatusView) findViewById(R.id.SimpleMultiStateView);
multipleStatusView.showLoading();
multipleStatusView.showEmpty();
multipleStatusView.showError();
multipleStatusView.showNoNetwork();
mMultiStateView.setOnRetryClickListener(v -> {
awvHelp.reload();
});
multipleStatusView.showContent();