发现了一个挺好用的LoadingLayout,网络错误,无数据,动态显示!
2017-01-22 14:43
465 查看
前言
项目里都会遇到几种页面,分别为加载中、无网络、无数据、出错四种情况,经常要使用,所以封成库引用了,方便使用,顺便分享出来。先看一下效果:
原理比较简单,继承FrameLayout,在xml渲染完成后,加上加载中、无网络、无数据、出错四个页面,根据需要控制显示哪一层,花了些时间,开了很多方法出来,支持很多属性的设置,算是比较实用,源码里已对各个方法的作用都加了注释,就不做过多解释了,项目GitHub地址:https://github.com/weavey/LoadingLayoutDemo,感兴趣的可以看看,欢迎指出问题。
使用方式
gradle引用:
使用说明
LoadingLayout支持全局配置,对所有使用到的地方都起效,需要在Application中配置,如下:
由于“加载中”的页面,可能每个App都不一样,因此,LoadingLayout支持自定义LoadingPage,如下:
LoadingLayout.getConfig()
.setLoadingPageLayout(R.layout.define_loading_page);
同时,为了适应个别界面的“特殊需求”,LoadingLayout也支持局部设置各种属性,仅对当前对象生效,不影响全局。如下:
为ReloadButton设置监听:
设置显示的页面:
最后,在xml里面使用:
注意:
(1)isFirstVisible属性用来控制contentView一开始是否隐藏,由于LoadingLayout原理是在xml渲染完成后在contentView上铺上三层View,因此,一开始如果不隐藏,等contentView渲染完成后调用: loadingLayout.setStatus(LoadingLayout.Loading);
会造成界面闪烁的效果,影响体验,因此默认将contentView隐藏,所以数据加载完成后一定要调用loadingLayout.setStatus(LoadingLayout.Success);,将contentView显示出来。这样也能解决未获取到数据的情况下,被用户看到杂乱无章的布局,个人还是比较喜欢默认隐藏contentView;
(2)为了方便管理,LoadingLayout只能有一个直属子View,类似ScrollView,添加两个直属子View会抛出异常throw new IllegalStateException(“LoadingLayout can host only one direct child”);;
(3)由于AS会直接将自定义View的特性反应在预览界面,所以在使用LoadingLayout的时候,会无法看到被LoadingLayout包裹住的布局(默认为gone),因此也可以将isFirstVisible属性暂时设为true,预览布局。
项目里都会遇到几种页面,分别为加载中、无网络、无数据、出错四种情况,经常要使用,所以封成库引用了,方便使用,顺便分享出来。先看一下效果:
原理比较简单,继承FrameLayout,在xml渲染完成后,加上加载中、无网络、无数据、出错四个页面,根据需要控制显示哪一层,花了些时间,开了很多方法出来,支持很多属性的设置,算是比较实用,源码里已对各个方法的作用都加了注释,就不做过多解释了,项目GitHub地址:https://github.com/weavey/LoadingLayoutDemo,感兴趣的可以看看,欢迎指出问题。
使用方式
gradle引用:
compile 'com.lai.weavey:loadinglayout:1.3.1'
使用说明
LoadingLayout支持全局配置,对所有使用到的地方都起效,需要在Application中配置,如下:
public class App extends Application { @Override public void onCreate() { super.onCreate(); LoadingLayout.getConfig() .setErrorText("出错啦~请稍后重试!") .setEmptyText("抱歉,暂无数据") .setNoNetworkText("无网络连接,请检查您的网络···") .setErrorImage(R.mipmap.define_error) .setEmptyImage(R.mipmap.define_empty) .setNoNetworkImage(R.mipmap.define_nonetwork) .setAllTipTextColor(R.color.gray) .setAllTipTextSize(14) .setReloadButtonText("点我重试哦") .setReloadButtonTextSize(14) .setReloadButtonTextColor(R.color.gray) .setReloadButtonWidthAndHeight(150,40); } }
由于“加载中”的页面,可能每个App都不一样,因此,LoadingLayout支持自定义LoadingPage,如下:
LoadingLayout.getConfig()
.setLoadingPageLayout(R.layout.define_loading_page);
同时,为了适应个别界面的“特殊需求”,LoadingLayout也支持局部设置各种属性,仅对当前对象生效,不影响全局。如下:
LoadingLayout loading = (LoadingLayout) findViewById(R.id.loading_layout); loading.setLoadingPage(R.layout.define_loading_page) .setEmptyText("暂无报告数据") .setErrorText("") .setNoNetworkText("") .setErrorImage(R.mipmap.ic_launcher) .setErrorTextSize(16) .setReloadButtonText("点我重新加载哦"); //等等
为ReloadButton设置监听:
loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() { @Override public void onReload(View v) { } });
设置显示的页面:
loadingLayout.setStatus(LoadingLayout.Loading);//加载中 loadingLayout.setStatus(LoadingLayout.Empty);//无数据 loadingLayout.setStatus(LoadingLayout.Error);//错误 loadingLayout.setStatus(LoadingLayout.No_Network);//无网络 loadingLayout.setStatus(LoadingLayout.Success);//加载成功
最后,在xml里面使用:
<com.weavey.loading.lib.LoadingLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:isFirstVisible="true"> <TextView android:background="@color/colorPrimary" android:visibility="visible" android:gravity="center" android:textColor="@android:color/white" android:layout_width="match_parent" android:layout_height="match_parent" android:text="ContentView"/> </com.weavey.loading.lib.LoadingLayout>
注意:
(1)isFirstVisible属性用来控制contentView一开始是否隐藏,由于LoadingLayout原理是在xml渲染完成后在contentView上铺上三层View,因此,一开始如果不隐藏,等contentView渲染完成后调用: loadingLayout.setStatus(LoadingLayout.Loading);
会造成界面闪烁的效果,影响体验,因此默认将contentView隐藏,所以数据加载完成后一定要调用loadingLayout.setStatus(LoadingLayout.Success);,将contentView显示出来。这样也能解决未获取到数据的情况下,被用户看到杂乱无章的布局,个人还是比较喜欢默认隐藏contentView;
(2)为了方便管理,LoadingLayout只能有一个直属子View,类似ScrollView,添加两个直属子View会抛出异常throw new IllegalStateException(“LoadingLayout can host only one direct child”);;
(3)由于AS会直接将自定义View的特性反应在预览界面,所以在使用LoadingLayout的时候,会无法看到被LoadingLayout包裹住的布局(默认为gone),因此也可以将isFirstVisible属性暂时设为true,预览布局。
相关文章推荐
- hbuider 中点击就显示出一个单选的列表 ,然后后台跨域向里面动态添加数据,注意里面的格式是json object
- listview只显示第一行数据的原因-layout参数错误
- 我们的应用或多或少都会从网络获取图片数据然后进行显示,下面就将实现一个这样的例子,获取网络中的图片!
- 对一个长度为100000条指令的程序进行集成测试期间记录如下面的数据: (A)7月1日集成测试开始没有发现错误 (B)8月2日总共改正了100个错误,此时MTTF=0.4H (C)9月1日:总共改正3
- TabLayout +ViewPager + Fragment : RecyclerView展示网络数据 + webView 显示网页
- echarts实现一个页面同时显示多个不同形状的图表/动态往图表中存放数据
- 从网络读取数据并动态的显示在ListView中
- iOS使用AFNetworking请求回来的网络数据,不能显示中文, 新建一个分类解决。
- tablayout上显示网络数据(xUtils请求)s
- 自学笔记:一个简单的神经网络,机器学习数字图片,可以显示错误的数字的图片
- LoadingLayout(一个可以随时切换加载中界面、错误界面、空界面、和主界面的控件)
- 解决vc++DEBUG ERROR问题前几天师弟调试程序的时候出现了这样一个错误,出错的位置是在delete [] 一个动态分配的数组时出现的。 经过调查发现错误是因为他之前在给数组赋值的时候越界了
- 从网络读取数据并动态的显示在ListView中
- winform,DataGridView绑定数据后更改列属性、动态创建(用一个控件显示多表适用)
- 从网络读取数据并动态的显示在ListView中
- 单击DataGrid的行,使这行的数据显示在这个页面的下面,有点像winform的一个列表
- 一个可动态编辑多条数据的JS
- 多个Select标签数据过滤的动态显示
- 单击DataGrid的行,使这行的数据显示在这个页面的下面,有点像winform的一个列表
- 一个通用的动态加载数据例子(IE5,NS6,Mozilla 1.3b)