首页 > 解决方案 > api调用时多个视图中的多个进度条

问题描述

当有多个调用时,我正在尝试为在片段/活动中添加和显示进度条进行通用实现。

请检查图片以供参考

有没有人有更好的解决方案,而不是参考两个进度条并切换其可见性?实现应该是通用的,可以应用于任何视图。

标签: androidapiprogress-barloadingprogress

解决方案


在你的项目中添加这个接口......

public interface RetrofitCallback {

    <T> void getSuccessCallBack(Response<T> response, int position);

    void getErrorCallBack(String message, int position);

}

在您的 Utility 或 RetrofitUtility 中添加这些方法。

public static <T> void callRetrofit(final Context context, final Fragment fragment, Call<T> call, final int position, final RetrofitCallback callback) {

        final ProgressDialog pDialog = CommonMethod.showProgressDialog(context, context.getResources().getString(R.string.loading));// progress for whole application
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                pDialog.dismiss();
                if (response.isSuccessful()) {
                    ResultBody result = (ResultBody) response.body();
                    if (result.isSuccess())
                        callback.getSuccessCallBack(response, position);
                    else
                        callback.getErrorCallBack(result.getMessage(), position);

                } else
                    Toast.makeText(context, response.message(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                pDialog.dismiss();
                if (CommonMethod.checkconnection(context)) { // checking internet connection
                    Toast.makeText(context, "Server_error", Toast.LENGTH_SHORT).show();
                } else {
                    CommonMethod.showconnectiondialog(context, fragment);
                }
            }
        });
    }

  public static Retrofit getClient() {

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl("XXXXXXXXXXXXX")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }

所有改造 API

public interface RetrofitApi {

    @POST("allData")
    Call<UserData> getAllData();

}

RetrofitCallback在你的 Activity 和 Fragment 中实现

像这样调用 Api

 Call<UserData> call = ApiClient.getClient().create(RetrofitApi.class).getAllData();
 ApiClient.callRetrofit(context, this, call, 0, this);

您将从下面的 RetrofitCallback 覆盖方法中获取数据

 @Override
    public <T> void getSuccessCallBack(Response<T> response, int position) {
        UserData userData = ((UserData) response.body());
        // do your operation over here 
    }

    @Override
    public void getErrorCallBack(String message, int position) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

这是所有模型文件....

ResultBody.class

public class ResultBody {
    private String message;
    private boolean success;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}

用户数据类

public class UserData extends ResultBody {

    private User userData;



    public User getUserData() {
        return userData;
    }

    public void setUserData(User user) {
        this.userData = user;
    }
}

让我知道你是否卡在任何地方......

相关帖子或问题

Android:动态传递模型类来改造回调


推荐阅读