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 4a721c2 commit 426d6df
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ compile ('com.zhousf.lib:okhttp3:2.9.1.2'){
* 支持异步延迟执行
* 2017-9-13
* Gson自动解析
* 2017-9-20
* 增加加载提示框示例

## 权限
```
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.okhttp3.R;

import java.util.ArrayList;
import java.util.List;

import base.networkstate.NetInfo;
import base.networkstate.NetworkStateListener;
import base.networkstate.NetworkStateReceiver;
Expand All @@ -23,7 +25,7 @@
* Activity基类:支持动态权限申请,网络状态监听
* @author zhousf
*/
public abstract class BaseActivity extends AppCompatActivity implements
public abstract class BaseActivity extends HttpActivity implements
ActivityCompat.OnRequestPermissionsResultCallback {

/**
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/base/BaseHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package base;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

/**
* 通用句柄
* @author zhousf
*/
public class BaseHandler extends Handler {

private CallBack callBack;

public interface CallBack{
void handleMessage(Message msg);
}

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

@Override
public void handleMessage(Message msg) {
if(null != callBack){
callBack.handleMessage(msg);
}
}

}
127 changes: 127 additions & 0 deletions app/src/main/java/base/HttpActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package base;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;

import java.io.IOException;

import base.view.LoadingDialog;

/**
* Activity基类:支持网络请求、加载提示框
* @author zhousf
*/
public class HttpActivity extends AppCompatActivity implements BaseHandler.CallBack {

private BaseHandler baseHandler;//Handler句柄

private LoadingDialog loadingDialog;//加载提示框

private final int SHOW_DIALOG = 0x001;
private final int DISMISS_DIALOG = 0x002;
private final int LOAD_SUCCEED = 0x003;
private final int LOAD_FAILED = 0x004;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

/**
* 同步请求
* @param info 请求信息体
* @return HttpInfo
*/
protected HttpInfo doHttpSync(HttpInfo info) {
//显示加载框
getBaseHandler().sendEmptyMessage(SHOW_DIALOG);
info = OkHttpUtil.getDefault(this).doSync(info);
if(info.isSuccessful()){
//显示加载成功
getBaseHandler().sendEmptyMessage(LOAD_SUCCEED);
}else {
//显示加载失败
getBaseHandler().sendEmptyMessage(LOAD_FAILED);
}
//隐藏加载框
getBaseHandler().sendEmptyMessageDelayed(DISMISS_DIALOG,1000);
return info;
}

/**
* 异步请求
* @param info 请求信息体
* @param callback 结果回调接口
*/
protected void doHttpAsync(HttpInfo info, final Callback callback){
getBaseHandler().sendEmptyMessage(SHOW_DIALOG);
OkHttpUtil.getDefault(this).doAsync(info, new Callback() {
@Override
public void onSuccess(HttpInfo info) throws IOException {
getBaseHandler().sendEmptyMessage(DISMISS_DIALOG);
callback.onSuccess(info);
}

@Override
public void onFailure(HttpInfo info) throws IOException {
getBaseHandler().sendEmptyMessage(DISMISS_DIALOG);
callback.onFailure(info);
}
});
}

protected LoadingDialog getLoadingDialog(){
if(null == loadingDialog){
loadingDialog = new LoadingDialog(this);
//点击空白处Dialog不消失
loadingDialog.setCanceledOnTouchOutside(false);
}
return loadingDialog;
}

/**
* 获取通用句柄,自动释放
*/
protected BaseHandler getBaseHandler(){
if(null == baseHandler){
baseHandler = new BaseHandler(this);
}
return baseHandler;
}


@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_DIALOG:
getLoadingDialog().showDialog();
break;
case LOAD_SUCCEED:
getLoadingDialog().succeed();
break;
case LOAD_FAILED:
getLoadingDialog().failed();
break;
case DISMISS_DIALOG:
getLoadingDialog().dismissDialog();
break;
default:
break;
}
}


@Override
protected void onDestroy() {
if(null != baseHandler)
baseHandler.removeCallbacksAndMessages(null);
super.onDestroy();
}
}
87 changes: 87 additions & 0 deletions app/src/main/java/base/view/LoadingDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package base.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.okhttp3.R;


/**
* Author : zhousf
* Description : 加载框
* Date : 2017/9/20.
*/
public class LoadingDialog extends Dialog {

private ImageView iv_load_result;// 加载的结果图标显示
private TextView tv_load;// 加载的文字展示
private ProgressBar pb_loading;// 加载中的图片

public LoadingDialog(Context context) {
super(context, R.style.loading_dialog_style);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.commom_loading_layout);
iv_load_result = (ImageView) findViewById(R.id.iv_load_result);
tv_load = (TextView) findViewById(R.id.tv_load);
pb_loading = (ProgressBar) findViewById(R.id.pb_loading);
iv_load_result.setVisibility(View.GONE);

}

public LoadingDialog text(String text){
if(tv_load != null && !TextUtils.isEmpty(text)){
tv_load.setText(text);
}
return this;
}

public void showDialog(){
text("加载中");
if(null != pb_loading && pb_loading.getVisibility() != View.VISIBLE){
pb_loading.setVisibility(View.VISIBLE);
}
if(null != iv_load_result){
iv_load_result.setVisibility(View.GONE);
}
show();
}

public void dismissDialog(){
dismiss();
}

// 加载成功
public void succeed() {
if(pb_loading != null)
pb_loading.setVisibility(View.GONE);
if(iv_load_result != null){
iv_load_result.setVisibility(View.VISIBLE);
iv_load_result.setImageResource(R.mipmap.load_suc_icon);
}
tv_load.setText("加载成功");
}

// 加载失败
public void failed() {
if(pb_loading != null)
pb_loading.setVisibility(View.GONE);
if(iv_load_result != null){
iv_load_result.setVisibility(View.VISIBLE);
iv_load_result.setImageResource(R.mipmap.load_fail_icon);
}
tv_load.setText("加载失败");
}



}
6 changes: 2 additions & 4 deletions app/src/main/java/com/okhttp3/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public void run() {
.setResponseEncoding(Encoding.UTF_8)//设置该接口服务器响应编码
.setRequestEncoding(Encoding.UTF_8)//设置该接口请求参数编码
.build();
OkHttpUtil.getDefault(this)
.doGetSync(info);
doHttpSync(info);
final String result = info.getRetDetail();
runOnUiThread(new Runnable() {
@Override
Expand All @@ -167,8 +166,7 @@ public void run() {
* 异步请求:回调方法可以直接操作UI
*/
private void async() {
OkHttpUtil.getDefault(this).doAsync(
HttpInfo.Builder()
doHttpAsync(HttpInfo.Builder()
.setUrl(url)
.setRequestType(RequestType.GET)//设置请求方式
.addHead("head", "test")//添加头参数
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/loading_black_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="10dp"/>
<solid android:color="#9D303132"/>
</shape>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/rotate_loading_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/loading_icon"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360"
/>
30 changes: 30 additions & 0 deletions app/src/main/res/layout/commom_loading_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="125dp"
android:layout_height="125dp"
android:background="@drawable/loading_black_bg"
android:gravity="center"
android:orientation="vertical" >

<ImageView
android:id="@+id/iv_load_result"
android:layout_width="50dp"
android:layout_height="50dp"
android:visibility="gone"/>

<ProgressBar
android:id="@+id/pb_loading"
style="@style/circleProgressBarStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:visibility="visible" />

<TextView
android:id="@+id/tv_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="加载中"
android:textColor="@android:color/white" />

</LinearLayout>
Binary file added app/src/main/res/mipmap-xhdpi/load_fail_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xhdpi/load_suc_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xhdpi/loading_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="loading_dialog_style" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimAmount">0.4</item>
</style>
<style name="circleProgressBarStyle">
<item name="android:indeterminateDrawable">@drawable/rotate_loading_icon</item>
<item name="android:indeterminateDuration">1500</item>
</style>

</resources>

0 comments on commit 426d6df

Please sign in to comment.