"
我是直接把dialog显示放在onSubscribe中,然后在onComplete或onError隐藏.但是我这样写不管哪一个界面dialog界面都是占满全屏,如何能够只让它显示在某个指定区域,如图是一个主界面,下面是四个切换按钮,想把dialog显示在红色区域内.
<span class=""img-wrap""><img referrerpolicy=""no-referrer"" data-src=""/img/bV9SV9?w=376&h=556"" src=""https://cdn.segmentfault.com/v-5e8d8dec/global/img/squares.svg"" alt=""图片描述"" title=""图片描述"" />
public abstract class HttpCallback<T> implements Observer<BaseResponse<T>> { public static final String TAG = HttpCallback.class.getCanonicalName();
private WeakReference<Activity> mActivity;
private boolean mShow;
private boolean mCancelable = false;
private boolean hasError = true;
private AlertDialog mDialog;
public HttpCallback(Activity activity) {
mActivity = new WeakReference<>(activity);
}
@Override
public void onSubscribe(Disposable d) {
if (mShow) {
showDialog();//默认不显示进度,需要显示重写onSubscribe方法,并showProgress为true
Log.d(TAG, "onSubscribe: 1103显示进度圈");
}else{
Log.d(TAG, "onSubscribe: 1103不显示进度圈");
}
}
@Override
public void onNext(BaseResponse<T> t) {
int code = t.getCode(); // 0 失败, 1 成功 , -1 token过期跳转到登录界面
// int code=-1; if (code == RequestUrl.CODE_SUCCESS) { hasError = false; onSuccess(t.getData(), t.getMsg()); } else if (code == RequestUrl.CODE_TOKEN_EXPIRE) { toLogin();//token失效,跳转到登录界面 } else { ToastUtils.showMessage(t.getMsg()); onFailure(); } }
@Override
public void onError(Throwable e) {
if (mDialog != null)
mDialog.dismiss();
if (e instanceof SocketTimeoutException) {
// 连接超时
onError(e, ErrorStatus.HTTP_TIMEOUT);
} else if (e instanceof ConnectException || e instanceof UnknownHostException) {
// 无网络
onError(e, ErrorStatus.HTTP_UNCONNECTED);
} else if (e instanceof HttpException) {
// 网络异常
// onError(e, ErrorStatus.HTTP_EXCEPTION.setErrorCode(((HttpException) e).code())); onError(e, ErrorStatus.HTTP_EXCEPTION); } else if (e instanceof JsonSyntaxException || e instanceof JSONException) { // 数据异常 onError(e, ErrorStatus.DATA_EXCEPTION); } else { // 其他错误 onError(e, ErrorStatus.OTHER_EXCEPTION); } onFinish(hasError); }
private void onError(Throwable e, final ErrorStatus otherException) {
if (e != null) {
e.printStackTrace();
}
mActivity.get().runOnUiThread(new Runnable() {
@Override
public void run() {
ToastUtils.showMessage(otherException.getErrorMessage(), Toast.LENGTH_SHORT);
}
});
}
@Override
public void onComplete() {
if (mDialog != null)
mDialog.dismiss();
onFinish(hasError);
}
public abstract void onSuccess(T t, String msg);
//除了成功与token失效的情况走这里
public void onFailure() {
}
//token失效
public void onTokenExpired() {
}
public void onFinish(boolean hasError) {
}
protected void showProgress(boolean show) {
mShow = show;
}
public void setCancelable(boolean cancelable) {
mCancelable = cancelable;
}
private void showDialog() {
mDialog = new AlertDialog.Builder(mActivity.get()).create();
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setCancelable(false);
mDialog.show();
Window window = mDialog.getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
window.setGravity(Gravity.CENTER);
window.setContentView(R.layout.loading);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
window.setAttributes(lp);
}
private void toLogin() {
// ToastUtils.showLongMessage("登录状态已过期,请重新登录"); AlertDialog.Builder builder = new AlertDialog.Builder(mActivity.get()); builder.setTitle("温馨提示") .setMessage("登录状态已过期,请重新登录") .setCancelable(false) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { UserManager.getInstance().logout();//清除保存数据 Intent loginIntent = new Intent(mActivity.get(), ThirdLoginActivity.class);//token失效,跳转到登录界面 loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // loginIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); mActivity.get().startActivity(loginIntent); } }).show(); } }
"
这个看上去和RxJava没关系。应该只需要自定义一个dialog,设置好对应的位置以及大小就可以了。或者不用dialog,用View去模拟dialog的行为也可以。
有关自定义dialog,请百度。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。