直接拿去用!每个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 ...
随机推荐
- Storm系列三: Storm消息可靠性保障
Storm系列三: Storm消息可靠性保障 在上一篇 Storm系列二: Storm拓扑设计 中我们已经设计了一个稍微复杂一点的拓扑. 而本篇就是在上一篇的基础上再做出一定的调整. 在这里先大概提一 ...
- 12、xamarin form中实现H5 网页唤醒微信支付的方法
在微信的支付中有种支付叫微信H5支付.方便用户在网页中轻松唤起微信进行支付. 当然微信不推荐大家使用这样的方式唤起微信支付.建议app还是使用正常的微信支付sdk即可 服务端与其他的建议参考微信支付官 ...
- js的事件学习笔记
目录 0.参考 1.事件流 冒泡传播 事件捕获 2.事件绑定--onclick接口 onclick类的接口,只能注册一个同类事件 onclick类的接口,使用button.onclick = null ...
- [摘]HttpContext, HttpRequest, HttpResponse, HttpRuntime, HttpServerUtility
[摘]http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html HttpRuntime HttpRuntime公开了一个静态方法 U ...
- EF基础知识小记七(拆分实体到多个表以及拆分表到多个实体)
一.拆分实体到多个表 1.在日常开发中,会经常碰到一些老系统,当客户提出一些新的需求,这些需求需要在原来的表的基础上加一些字段,大多数人会选择通过给原表添加字段的方式来完成这些需求,方法,虽然可行,但 ...
- Linq基础知识小记三
1.子查询 Linq中的子查询思想和Sql中的子查询其实差不多, 对于方法语法,一个子查询包含在另一个子查询的Lambda表达式中,代码如下: string[] names = { "Jam ...
- ActiveRecord::Fixture::FixtureError: table "users" has no column named "activated_at".
window 7+ruby2.33+rails5.0. 在测试的时候 rails test 报固件fixture错误: 没有某列字段存在 虽然可以直接通过开发框架去修改字段,但是开发过程中应该通过迁移 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C/C++ -- Gui编程 -- Qt库的使用 -- 使用自定义类
1.新建空Qt工程 2.新建C++类HelloQt 3.新建ui文件,添加部件,重命名主窗体(对话框)类名HelloQt,构建生成ui头文件 4.修改头文件helloqt.h #ifndef HELL ...
- java 之 异常处理小结
1 :分类 检查异常(checked,受检) 运行异常(unchecked) 2:捕获异常 try/catch try/catch/finally try/finally try{ //受保 ...