一个支持多种状态的自定义View,可以方便的切换到:
- 加载中视图
- 错误视图
- 空数据视图
- 网络异常视图
- 内容视图
使用
dependencies {
implementation 'com.classic.common:multiple-status-view:1.7'
}
示例
<com.classic.common.MultipleStatusView
android:id="@+id/multiple_status_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:loadingView="@layout/custom_loading_view"
app:emptyView="@layout/custom_empty_view"
app:errorView="@layout/custom_error_view"
app:noNetworkView="@layout/custom_no_network_view"
app:contentView="@layout/main_content"/>
MultipleStatusView multipleStatusView = (MultipleStatusView) findViewById(R.id.multiple_status_view);
//显示加载中视图
multipleStatusView.showLoading();
// mMultipleStatusView.showLoading("自定义提示内容");
// mMultipleStatusView.showLoading(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showLoading(R.layout.xxx, layoutParams);
// multipleStatusView.showLoading(customView, layoutParams);
//显示空视图
multipleStatusView.showEmpty();
// mMultipleStatusView.showEmpty("自定义提示内容");
// mMultipleStatusView.showEmpty(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showEmpty(R.layout.xxx, layoutParams);
// multipleStatusView.showEmpty(customView, layoutParams);
//显示错误视图
multipleStatusView.showError();
// mMultipleStatusView.showError("自定义提示内容");
// mMultipleStatusView.showError(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showError(R.layout.xxx, layoutParams);
// multipleStatusView.showError(customView, layoutParams);
//显示无网络视图
multipleStatusView.showNoNetwork();
// mMultipleStatusView.showNoNetwork("自定义提示内容");
// mMultipleStatusView.showNoNetwork(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showNoNetwork(R.layout.xxx, layoutParams);
// multipleStatusView.showNoNetwork(customView, layoutParams);
//显示内容视图
multipleStatusView.showContent();
// multipleStatusView.showContent(R.layout.xxx, layoutParams);
// multipleStatusView.showContent(customView, layoutParams);
//设置重试视图点击事件
multipleStatusView.setOnRetryClickListener(onRetryClickListener);
//设置视图状态切换监听事件
mMultipleStatusView.setOnViewStatusChangeListener(OnViewStatusChangeListener);
/**
* 获取当前view的状态
* MultipleStatusView.STATUS_LOADING //当前为加载中视图
* MultipleStatusView.STATUS_EMPTY //当前为空视图
* MultipleStatusView.STATUS_ERROR //当前为错误视图
* MultipleStatusView.STATUS_NO_NETWORK//当前为无网络视图
* MultipleStatusView.STATUS_CONTENT //当前为内容视图
*/
int viewStatus = multipleStatusView.getViewStatus();
MultipleStatusView
继承自 RelativeLayout
,所以内容视图也可以直接写在 MultipleStatusView
内部
<com.classic.common.MultipleStatusView
android:id="@+id/multiple_status_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:emptyView="@layout/custom_empty_view"
app:errorView="@layout/custom_error_view"
app:loadingView="@layout/custom_loading_view"
app:noNetworkView="@layout/custom_no_network_view">
<ImageView
android:id="@+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:layout_marginRight="16dp"
android:src="@drawable/test"/>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/image"
android:layout_toRightOf="@id/image"
android:text="内容视图111111"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/image"
android:layout_toRightOf="@id/image"
android:text="内容视图222222"/>
</com.classic.common.MultipleStatusView>
注意事项
必须按如下规则设置对应视图根节点的
view id
,否则会导致试图重叠
必须按如下规则设置对应视图根节点的view id
,否则会导致试图重叠
必须按如下规则设置对应视图根节点的view id
,否则会导致试图重叠
1. 如果使用自定义属性
app:emptyView="@layout/..."
app:errorView="@layout/..."
app:loadingView="@layout/..."
app:noNetworkView="@layout/..."
需要设置:
- 加载中视图的id必须为:
loading_view
- 空视图的id必须为:
empty_view
- 错误视图的id必须为:
error_view
- 无网络视图的id必须为:
no_network_view
- 自定义提示文本的id必须为:
status_hint_content
view
进行重试, 需要设置:
2. 如果需要点击某个 - 空视图内对应的view id:
empty_retry_view
- 错误视图内对应的view id:
error_retry_view
- 无网络视图内对应的view id:
no_network_retry_view
new
关键字创建自定义视图时,请设置 id
3. 使用 TextView tv = new TextView(getApplicationContext());
tv.setId(Utils.generateViewId());
tv.setText(text);
更多使用方法详见 demo
示例:
贡献者
感谢以下人员贡献的代码
关于
- Blog: http://blog.csdn.net/qy1387
- Email: [email protected]
License
Copyright 2015 classic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.