如何在每个Retrofit 2.0
API调用中显示进度条,而无需在每个活动中创建进度条,显示和解除。进度条应显示每当点击API时,如果我们在onResponse
或onFailure
上收到回复,则应该忽略它。
我试过了:
ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage("Loading...");
mProgressDialog.show();
retrofitService.login(new SignInRequest(email, password),
new Callback<SignInResponse>() {
@Override
public void onResponse(Call<SignInResponse> call, Response<SignInResponse> response) {
if (mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
@Override
public void onFailure(Call<SignInResponse> call, Throwable t) {
if (mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
});
但是每当我进行API调用时,这段代码都必须在任何地方粘贴。我不想要重复的代码。
答案 0 :(得分:7)
您的Activity中的另一个内部类可能会保存您。
{{#each languages as |lang| }}
<li><a href="/all/language/{{lang.url}}">{{lang.title}}</a></li>
{{/each}}
发送请求时:
class MyCallBack implements CallBacks<T>{
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
@Override
public void onFailure(Call<SignInResponse> call, Throwable t) {
if (mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
}
答案 1 :(得分:4)
正如@Sourabh所建议的那样,我在每次API调用期间都使用了Base活动并调用了简单方法。在BaseActivity
,
public void showDialog() {
if(mProgressDialog != null && !mProgressDialog.isShowing())
mProgressDialog.show();
}
public void hideDialog() {
if(mProgressDialog != null && mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
在您的儿童活动中,您可以直接致电showDialog()
和hideDialog()
来展示和解除对话。
答案 2 :(得分:3)
根据Shubham和peter的答案,我写了一个这样的课:
class CustomCallBack<T> implements Callback<T> {
private ProgressDialog mProgressDialog;
Context context;
CustomCallBack(Context context) {
this.context = context;
mProgressDialog = new ProgressDialog(context);
((Activity) context).getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
}
}
我希望它会对你有所帮助。
答案 3 :(得分:1)
无耻推广
我为此创建了RxLoading library,它可以做到这一点以及更多,
你可以做这样的事情:networkCall().compose(RxLoading.<>create(loadingLayout)).subscribe(...);
它由2个类组成,一个自定义视图(loadingLayout)和一个将transformer粘合在一起的RxLoading,您可以选择使用它们中的任何一个或两个。
如果您想要一个进度条来统治它们,您可以根据应用程序的结构有多种方法来实现它:
RxLoading还支持空状态和错误状态(内置重试机制,因此请务必检查此选项)
您可以在GitHub page上找到更多信息。
答案 4 :(得分:0)
教程示例:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
requestWindowFeature(Window.FEATURE_PROGRESS);
ArrayAdapter<Question> arrayAdapter =
new ArrayAdapter<Question>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
new ArrayList<Question>());
setListAdapter(arrayAdapter);
setProgressBarIndeterminateVisibility(true);
setProgressBarVisibility(true);
}