直接拿去用!每个App都会用到的LoadingLayout

前言
项目里都会遇到几种页面,分别为加载中、无网络、无数据、出错四种情况,经常要使用,所以封成库引用了,方便使用,顺便分享出来。先看一下效果:

原理比较简单,继承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,预览布局。
直接拿去用!每个App都会用到的LoadingLayout的更多相关文章
- 项目- Vue全家桶实战去哪网App
最近在学习Vue,花了几天时间跟着做了这个项目,算是对学习Vue入门的一个总结,欢迎同学们star 去哪网APP
- Vue2.5 开发去哪儿网App
Vue2.5开发去哪儿网App 技术栈和主要框架
- Vue2.5开发去哪儿网App 首页开发
主页划 5 个组件,即 header icon swiper recommend weekend 一. header区域开发 1. 安装 stylus npm install stylus --s ...
- Vue2.5开发去哪儿网App 第三章笔记 上
1. vue 生命周期函数 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 ...
- Vue2.5开发去哪儿网App 城市列表开发之 Vuex实现数据共享及高级使用
一,数据共享 1. 安装: npm install vuex --save 2. 在src目录下 新建state文件夹,新建index.js文件 3. 创建一个 store import Vue f ...
- Vue2.5开发去哪儿网App 第五章笔记 下
1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...
- Vue2.5开发去哪儿网App 第五章笔记 上
1.css动画原理 .fade-enter{ opacity: 0; } .fade-enter-active{ transition: opacity 2s; } .fade-leave-to{ o ...
- Vue2.5开发去哪儿网App 第四章笔记 下
1.解决非父子组件之间的传值问题 非父子组件传值(Bus/总线/发布订阅模式/观察者模式) 给 Vue类上挂在一个属性,然后创建vue实例时,实例就拥有了这个属性 Vue.prototype.bus ...
- Vue2.5开发去哪儿网App 第四章笔记 上
一 . 组件细节知识点 1. 解决组件在h5中编码规范 例如 : table , ul , ol 等等 <table> <tbody> <row></r ...
随机推荐
- Python字典按值排序、包含字典的列表按字典值排序的方法
operator.itemgetter函数 operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),要注意,operator.i ...
- C++的开源跨平台日志库glog学习研究(一)
作为C++领域中为数不多的好用.高效的.跨平台的日志工具,Google的开源日志库glog也算是凤毛麟角了.glog 是一个C++实现的应用级日志记录框架,提供了C++风格的流操作. 恰巧趁着五一我也 ...
- Java之集合(二十六)ConcurrentSkipListMap
转载请注明源出处:http://www.cnblogs.com/lighten/p/7542578.html 1.前言 一个可伸缩的并发实现,这个map实现了排序功能,默认使用的是对象自身的compa ...
- Java之集合(十一)IdentityHashMap
转载请注明源出处:http://www.cnblogs.com/lighten/p/7381905.html 1.前言 查看JDK源码总是能发现一些新东西,IdentityHashMap也是Map的一 ...
- ubuntu 下 重启 mongo 后 遇到蛋疼问题。
以后,切忌 mongo 正常关闭后 ,再重启ubuntu. 否则后果这是很严重. 2014.8.6日 PM 6点. 网站莫名打不开了,全部是空白,又是老问题. 幸亏 及时发现,那就重启下. 蛋疼,重启 ...
- android 使用lint + studio ,排查客户端无用string,drawable,layout资源
在项目中点击右键(或者菜单中的Analyze),在出现的右键菜单中有“Analyze” --> “run inspaction by Name ...”.在弹出的搜索窗口中输入想执行的检查类型, ...
- web前端html快速入门
HTML 学前端之间不得不知道一个网站:http://www.w3school.com.cn/ 网上有很多教程关于前端的,写的特别详细,也写的特别好.我们应该要自已理解,一些相应的前端的知识,不能只是 ...
- pandas 去除NAN
df = df.dropna(axis=0, how='any')
- JavaI/O系统2
数据流: DataInputStream,DataOutputStream.可以用于与计算机无关的格式读写java的基本数据类型以及String对象 对象流: ObjectInputStream,Ob ...
- docker搭建私有registry
搭建docker的私有registry 1. registry简介 Docker在2015年推出了distribution项目,即Docker Registry 2.相比于old registry ...