這幾天利用一些時間,整理了一下,寫了一個簡單的工具類,用來快速的顯示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方法;