这几天利用一些时间,整理了一下,写了一个简单的工具类,用来快速的显示LoadingView或者EmptyView,以前都是定义一个layout,根布局为FrameLayout,再在layout布局文件里面添加LoadingView或者EmptyView,然后在Activity里面设置visible属性,代码太冗余,所以在这种情况下催生了我这个小项目;

代码拖管地址:

https://github.com/a284628487/EasyLoading

现在使用这个工具类,一切都变得很简单,不需要再在Activity中或者Fragment中去定义一个成员变量,然后初始化、显示、隐藏;

现在只需要一句代码就搞定

显示LoadingView

ActivityTool.showLoading(Activity activity, int layoutId)
ActivityTool.showLoading(Activity activity, View view)
ActivityTool.showLoading(Activity activity, int layoutId, boolean dimBackground)
ActivityTool.showLoading(Activity activity, View view, boolean dimBackground)

显示EmptyView

ActivityTool.showEmpty(Activity activity, int layoutId)
ActivityTool.showEmpty(Activity activity, View view)

隐藏LoadingView或EmptyView

ActivityTool.dismiss(Activity activity)

  显示的时候,只需要传入你要显示的view或者一个layoutid,LoadingView / EmptyView就显示出来了,看到这里,可能有人会想,那我还不是得传入一个View或者定义一个layout布局,确实如此。但是,在这种情况下,我们的LoadingView / EmptyView和Activity和Fragment是分开管理了的,没有任何关联,在你的Activity中和Fragment中,是看不到LoadingView / EmptyView的,也不需要在layout中定义多个ViewStub,使Fragment和Activity更加纯净,没有干扰信息;

注意:在Fragment中使用时,要求Fragment的所持有的view为FrameLayout,有两种方法,参见上面github代码托管地址;

  To showing LoadingView or EmptyView in a Fragment, the Fragment’s rootView must be a FrameLayout or FrameLayout’s subclass, you can define a xml-layout which rootView is FrameLayout for the Fragment or just make your Fragment extends LoadingSupportFragment / LoadingFragment and achieve the method contentViewLayoutId() to return your xml-layoutId, and you don’t need to override the method onCreateView(); yeah, you can show the EmptyView or LoadingView simply;

  为了让Fragment能够显示LoadingView或EmptyView,Fragment所创建的view必须是FrameLayout或者FrameLayout的子类,这里有两种快捷的方法,一是同往常一样,定义一个layout布局文件然后让Fragment创建view,但该layout的rootView必须为FrameLayout;二是继承LoadingFragment或者LoadingSupportFragment,并且实现方法contentViewLayoutId(),在该方法中返回自定义的layout布局文件,该布局文件则没有rootView的限制,rootView可以是任意的控件,并且,使用这种方法也就不需要再重写Fragment的onCreateView方法;

Android showing LoadingView or EmptyView for Activity or Fragment的更多相关文章

  1. Android:手把手教你 实现Activity 与 Fragment 相互通信,发送字符串信息(含Demo)

    前言Activity 与 Fragment 的使用在Android开发中非常多今天,我将主要讲解 Activity 与 Fragment 如何进行通信,实际上是要解决两个问题: Activity 如何 ...

  2. Android 数据传递(二)Activity与fragment之间的通信

    在网上找到了一篇总结的非常好的文章,我这里就贴出他的博文地址.自己就不再写这个方面的总结了. Activity与Fragment通信(99%)完美解决方案

  3. Android开发系列(一)Activity与Fragment获取屏幕获取屏幕像素的不同方式

    Activity中常用的获取屏幕像素代码: //获取屏幕像素相关信息 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getD ...

  4. [Android] Android Butterknife 8.8.1 在 Activity 和 Fragment 、 Adapter 中的使用

    ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤.是大神JakeW ...

  5. Android窗口管理服务WindowManagerService切换Activity窗口(App Transition)的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8596449 在Android系统中,同一时刻只 ...

  6. Android PopupWindow Dialog 关于 is your activity running 崩溃详解

    Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...

  7. android 关闭多个或指定activity

    打开了.activityA,B,C,D,...然后到E一起关闭前面所有activity(转自:http://blog.csdn.net/lengguoxing/article/details/4214 ...

  8. 查看Android应用的package name和activity name方面

    使用android自动化测试工具monkeyrunner启动应用时,需要填写被测程序的包名和启动的Activity,以下有两种查看应用包名package和入口activity名称的方法:方法一:使用a ...

  9. Android Studio 小提示,新建Activity

    Android Studio是在google I/O大会上新发布的一个IDE,基于IntelliJ,Android开发除了Eclipse之外又多了一种选择. 在Android Studio中如何在当前 ...

随机推荐

  1. [Java Web] 2、Web开发中的一些架构

    1.企业开发架构: 企业平台开发大量采用B/S开发模式,不管采用何种动态Web实现手段,其操作形式都是一样的,其核心操作的大部分都是围绕着数据库进行的.但是如果使用编程语言进行数据库开发,要涉及很多诸 ...

  2. 博客搬家了,欢迎访问 http://blog.csdn.net/yinpengxiang/

    博客搬家了,欢迎访问 http://blog.csdn.net/yinpengxiang/

  3. 无线客户端框架设计(2):项目结构的设计(iOS篇)

    本文附带源码:YoungHeart-Chapter-02.zip 在设计任何一个框架之前,都应规划好项目结构. 假定Git作为我们的项目管理工具.我们要建立两个仓库,一个用于存放我们的框架,另一个用于 ...

  4. 标签简化Spring-MVC配置

    新填入@RequestMapping标签 和@org.springframework.stereotype.Controller标签 这样做就是通过标签来简化之前,对HandlerMapping的配置 ...

  5. Atitit.js图表控件总结

    Atitit.js图表控件总结 1. 为什么要使用图表1 2. 图表分类1 3. 数据可视化的优点1 4. 流行的js图表类库1 5. 参考2 1. 为什么要使用图表 因为要可视化 2. 图表分类 条 ...

  6. 修改JSONArray里所有key的值

    下面举一个代码的列子目的是实现如下功能: [{"userId":1,"userName":"plf"},{"userId" ...

  7. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...

  8. 从Windows XP系统迁移到Windows 7,Windows 8开始

    Microsoft在2014年4月8日结束了Windows XP的支持.您的公司准备好了吗?如果您还没有迁移到Windows 7或8,那就要抓紧时间了.从现在起将不再向XP系统提供安全修补程序,而仍然 ...

  9. ASP.NET Web API中的参数绑定总结

    ASP.NET Web API中的action参数类型可以分为简单类型和复杂类型. HttpResponseMessage Put(int id, Product item) id是int类型,是简单 ...

  10. Word中怎么设置标题的自动编号

    转自: http://jingyan.baidu.com/article/4f7d5712c39c0a1a201927ea.html