Android showing LoadingView or EmptyView for Activity or Fragment

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

代码拖管地址:github

https://github.com/a284628487/EasyLoadingide

 

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

如今只须要一句代码就搞定布局

显示LoadingViewspa

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)

显示EmptyViewcode

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

隐藏LoadingView或EmptyViewxml

ActivityTool.dismiss(Activity activity)

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

 

注意:在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方法;

相关文章
相关标签/搜索