Skip to content

Repository files navigation

Gloading

中文文档

Show global loading status view in a low coupling way for Android App.

JavaDocs | Download Demo

Latest Version: Download

Lightweight: aar is less than 6KB, just 170 code lines and 104 comment lines within only 1 java file.

Design as Adapter pattern,with good compatibility: most third-party LoadingViews can be used as Gloading views in the Adapter

Demo

Wrap activity page

Load successLoad failed and click retryLoad success with empty dataThis loading status UI is special

Wrap view(s)

Wrap single viewWrap viewsWrap in GridViewWrap in RecyclerView
No words below

Usage

compile 'com.billy.android:gloading:1.0.0'

1. Provide global loading status views

For global usage, create an Adapter to provide views for all status via getView(...) method

Note: Activity/Fragment/View reused in 2 or more apps with different loading status views?

Just provide a different Adapter for each app.
No need to change any usage code

demo

publicclassGlobalAdapterimplementsGloading.Adapter {
@OverridepublicViewgetView(Gloading.Holderholder, ViewconvertView, intstatus) {
GlobalLoadingStatusViewloadingStatusView = null;
//reuse the old view, if possibleif (convertView != null && convertViewinstanceofGlobalLoadingStatusView) {
loadingStatusView = (GlobalLoadingStatusView) convertView;
}
if (loadingStatusView == null) {
loadingStatusView = newGlobalLoadingStatusView(holder.getContext(), holder.getRetryTask());
}
loadingStatusView.setStatus(status);
returnloadingStatusView;
}
classGlobalLoadingStatusViewextendsRelativeLayout {
publicGlobalLoadingStatusView(Contextcontext, RunnableretryTask) {
super(context);
//init view ...
}
publicvoidsetStatus(intstatus) {
//change ui by different status...
}
}
}

See demo code

2. Init Gloading by Adapter before use it

Gloading.initDefault(newGlobalAdapter());

Note: Use AutoRegister to decoupling this step.

3. Show global loading status views in all pages

3.1 Wrap something and return a Gloading.Holder object

//Gloading wrapped whole activity, wrapper view: android.R.id.contentGloading.Holderholder = Gloading.getDefault().wrap(activity);
//with load failed retry taskGloading.Holderholder = Gloading.getDefault().wrap(activity).withRetry(retryTask);

or

//Gloading will create a FrameLayout to wrap itGloading.Holderholder = Gloading.getDefault().wrap(view);
//with load failed retry taskGloading.Holderholder = Gloading.getDefault().wrap(view).withRetry(retryTask);

3.1.1 Cover something within RelativeLayout (or ConstraintLayout) (since: v1.1.0)

//Gloading will create a FrameLayout to cover itGloading.Holderholder = Gloading.getDefault().cover(view);
//with load failed retry taskGloading.Holderholder = Gloading.getDefault().cover(view).withRetry(retryTask);

3.2 Show status views for loading/loadFailed/empty/... by Gloading.Holder

//show loading status view by holderholder.showLoading() //show load success status view by holder (frequently, hide gloading)holder.showLoadSuccess()
//show load failed status view by holder (frequently, needs retry task)holder.showFailed()
//show empty status view by holder. (load completed, but data is empty)holder.showEmpty()

More Gloading.Holder APIs

Practice

1. Wrap into BaseActivity/BaseFragment

publicabstractclassBaseActivityextendsActivity {
protectedGloading.HoldermHolder;
/** * make a Gloading.Holder wrap with current activity by default * override this method in subclass to do special initialization */protectedvoidinitLoadingStatusViewIfNeed() {
if (mHolder == null) {
//bind status view to activity root view by defaultmHolder = Gloading.getDefault().wrap(this).withRetry(newRunnable() {
@Overridepublicvoidrun() {
onLoadRetry();
}
});
}
}
protectedvoidonLoadRetry() {
// override this method in subclass to do retry task
}
publicvoidshowLoading() {
initLoadingStatusViewIfNeed();
mHolder.showLoading();
}
publicvoidshowLoadSuccess() {
initLoadingStatusViewIfNeed();
mHolder.showLoadSuccess();
}
publicvoidshowLoadFailed() {
initLoadingStatusViewIfNeed();
mHolder.showLoadFailed();
}
publicvoidshowEmpty() {
initLoadingStatusViewIfNeed();
mHolder.showEmpty();
}
}

2. Call super methods inside subclasses

publicclassGlobalFailedActivityextendsBaseActivity {
privateImageViewimageView;
privateStringpicUrl;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
//do sth init...loadData();
}
privatevoidloadData() {
showLoading();
loadDataAndCallback(newCallback() {
publicvoidsuccess(Datadata) {
if (isEmpty(data)) {
showEmpty();
} else {
//do sth with data...showLoadSuccess();
}
}
publicvoidfailed() {
//do sth...showLoadFailed();
}
});
}
@OverrideprotectedvoidonLoadRetry() {
loadData();
}
//other codes...
}

Debug mode

//debug mode. if set true, logs will print into logcatGloading.debug(trueOrFalse);

Thanks

Pictures in demo app all from: https://www.thiswaifudoesnotexist.net/

Global LoadingView pictures used in demo all from: https://www.iconfont.cn/

Special LoadingView UI used in demo from: https://github.com/ldoublem/LoadingView/

About

Show global loading status view in a low coupling way for Android App(超轻量级,深度解耦Android App中全局加载中、加载失败及空数据视图)

Resources

Stars

1.4k stars

Watchers

22 watching

Forks

Releases

Packages

Contributors

Languages