android离线数据加载失败怎么办,深度解耦Android App中全局加载中、加载失败及空数据视图)...

Gloading

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

Latest Version: 68747470733a2f2f6170692e62696e747261792e636f6d2f7061636b616765732f68656c6c6f62696c6c792f616e64726f69642f676c6f6164696e672f696d616765732f646f776e6c6f61642e737667

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 success

Load failed and click retry

Load success with empty data

This loading status UI is special

116f3de56100b618053a63ba0fc59a86.gif

27e8d475dd8eb2cdb7fc526a0748ffd0.gif

80e49b59c011ef7cd53b1650e7484183.gif

198e6bd2954f494eee5bbb1c18de958f.gif

Wrap view(s)

Wrap single view

Wrap views

Wrap in GridView

Wrap in RecyclerView

No words below

50994ab802580f8aab1d159deb4e302d.gif

30f6320f3b97e93b23bec3c6b79a4ddd.gif

eb3fec0883d730a582df41279b0e74fe.gif

bfcb72cc2f3a5f81579efd00d1b999d6.gif

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

public class GlobalAdapter implements Gloading.Adapter {

@Override

public View getView(Gloading.Holder holder, View convertView, int status) {

GlobalLoadingStatusView loadingStatusView = null;

//reuse the old view, if possible

if (convertView != null && convertView instanceof GlobalLoadingStatusView) {

loadingStatusView = (GlobalLoadingStatusView) convertView;

}

if (loadingStatusView == null) {

loadingStatusView = new GlobalLoadingStatusView(holder.getContext(), holder.getRetryTask());

}

loadingStatusView.setStatus(status);

return loadingStatusView;

}

class GlobalLoadingStatusView extends RelativeLayout {

public GlobalLoadingStatusView(Context context, Runnable retryTask) {

super(context);

//init view ...

}

public void setStatus(int status) {

//change ui by different status...

}

}

}

2. Init Gloading by Adapter before use it

Gloading.initDefault(new GlobalAdapter());

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.content

Gloading.Holder holder = Gloading.getDefault().wrap(activity);

//with load failed retry task

Gloading.Holder holder = Gloading.getDefault().wrap(activity).withRetry(retryTask);

or

//Gloading will create a FrameLayout to wrap it

Gloading.Holder holder = Gloading.getDefault().wrap(view);

//with load failed retry task

Gloading.Holder holder = Gloading.getDefault().wrap(view).withRetry(retryTask);

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

//show loading status view by holder

holder.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()

Practice

1. Wrap into BaseActivity/BaseFragment

public abstract class BaseActivity extends Activity {

protected Gloading.Holder mHolder;

/**

* make a Gloading.Holder wrap with current activity by default

* override this method in subclass to do special initialization

*/

protected void initLoadingStatusViewIfNeed() {

if (mHolder == null) {

//bind status view to activity root view by default

mHolder = Gloading.getDefault().wrap(this).withRetry(new Runnable() {

@Override

public void run() {

onLoadRetry();

}

});

}

}

protected void onLoadRetry() {

// override this method in subclass to do retry task

}

public void showLoading() {

initLoadingStatusViewIfNeed();

mHolder.showLoading();

}

public void showLoadSuccess() {

initLoadingStatusViewIfNeed();

mHolder.showLoadSuccess();

}

public void showLoadFailed() {

initLoadingStatusViewIfNeed();

mHolder.showLoadFailed();

}

public void showEmpty() {

initLoadingStatusViewIfNeed();

mHolder.showEmpty();

}

}

2. Call super methods inside subclasses

public class GlobalFailedActivity extends BaseActivity {

private ImageView imageView;

private String picUrl;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//do sth init...

loadData();

}

private void loadData() {

showLoading();

loadDataAndCallback(new Callback() {

public void success(Data data) {

if (isEmpty(data)) {

showEmpty();

} else {

//do sth with data...

showLoadSuccess();

}

}

public void failed() {

//do sth...

showLoadFailed();

}

});

}

@Override

protected void onLoadRetry() {

loadData();

}

//other codes...

}

Debug mode

//debug mode. if set true, logs will print into logcat

Gloading.debug(trueOrFalse);

Thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值