Skip to content

Commit

Permalink
增加加载框示例
Browse files Browse the repository at this point in the history
  • Loading branch information
MrZhousf committed Sep 20, 2017
1 parent 426d6df commit 4808385
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/base/BaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public interface CallBack{
void handleMessage(Message msg);
}

public BaseHandler(CallBack callBack){
super(Looper.getMainLooper());
public BaseHandler(CallBack callBack,Looper looper){
super(looper);
this.callBack = callBack;
}

Expand Down
31 changes: 16 additions & 15 deletions app/src/main/java/base/HttpActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base;

import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -19,7 +20,7 @@
*/
public class HttpActivity extends AppCompatActivity implements BaseHandler.CallBack {

private BaseHandler baseHandler;//Handler句柄
private BaseHandler mainHandler;

private LoadingDialog loadingDialog;//加载提示框

Expand All @@ -41,17 +42,17 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
*/
protected HttpInfo doHttpSync(HttpInfo info) {
//显示加载框
getBaseHandler().sendEmptyMessage(SHOW_DIALOG);
getMainHandler().sendEmptyMessage(SHOW_DIALOG);
info = OkHttpUtil.getDefault(this).doSync(info);
if(info.isSuccessful()){
//显示加载成功
getBaseHandler().sendEmptyMessage(LOAD_SUCCEED);
getMainHandler().sendEmptyMessage(LOAD_SUCCEED);
}else {
//显示加载失败
getBaseHandler().sendEmptyMessage(LOAD_FAILED);
getMainHandler().sendEmptyMessage(LOAD_FAILED);
}
//隐藏加载框
getBaseHandler().sendEmptyMessageDelayed(DISMISS_DIALOG,1000);
getMainHandler().sendEmptyMessageDelayed(DISMISS_DIALOG,1000);
return info;
}

Expand All @@ -61,17 +62,17 @@ protected HttpInfo doHttpSync(HttpInfo info) {
* @param callback 结果回调接口
*/
protected void doHttpAsync(HttpInfo info, final Callback callback){
getBaseHandler().sendEmptyMessage(SHOW_DIALOG);
getMainHandler().sendEmptyMessage(SHOW_DIALOG);
OkHttpUtil.getDefault(this).doAsync(info, new Callback() {
@Override
public void onSuccess(HttpInfo info) throws IOException {
getBaseHandler().sendEmptyMessage(DISMISS_DIALOG);
getLoadingDialog().dismiss();
callback.onSuccess(info);
}

@Override
public void onFailure(HttpInfo info) throws IOException {
getBaseHandler().sendEmptyMessage(DISMISS_DIALOG);
getLoadingDialog().dismiss();
callback.onFailure(info);
}
});
Expand All @@ -89,11 +90,11 @@ protected LoadingDialog getLoadingDialog(){
/**
* 获取通用句柄,自动释放
*/
protected BaseHandler getBaseHandler(){
if(null == baseHandler){
baseHandler = new BaseHandler(this);
protected BaseHandler getMainHandler(){
if(null == mainHandler){
mainHandler = new BaseHandler(this, Looper.getMainLooper());
}
return baseHandler;
return mainHandler;
}


Expand All @@ -110,7 +111,7 @@ public void handleMessage(Message msg) {
getLoadingDialog().failed();
break;
case DISMISS_DIALOG:
getLoadingDialog().dismissDialog();
getLoadingDialog().dismiss();
break;
default:
break;
Expand All @@ -120,8 +121,8 @@ public void handleMessage(Message msg) {

@Override
protected void onDestroy() {
if(null != baseHandler)
baseHandler.removeCallbacksAndMessages(null);
if(null != mainHandler)
mainHandler.removeCallbacksAndMessages(null);
super.onDestroy();
}
}
5 changes: 1 addition & 4 deletions app/src/main/java/base/view/LoadingDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public LoadingDialog text(String text){
return this;
}

//显示对话框
public void showDialog(){
text("加载中");
if(null != pb_loading && pb_loading.getVisibility() != View.VISIBLE){
Expand All @@ -56,10 +57,6 @@ public void showDialog(){
show();
}

public void dismissDialog(){
dismiss();
}

// 加载成功
public void succeed() {
if(pb_loading != null)
Expand Down

0 comments on commit 4808385

Please sign in to comment.