自定义进度对话框不隐藏

时间:2016-07-04 09:19:24

标签: android progressdialog android-progressbar

我试图在TabLayout的2个片段中使用this第三方库来显示自定义进度条。我在后台显示进度条我正在进行排球请求。问题是即使从Web服务加载结果后,进度条也没有隐藏。当我使用常规progressdialog与TabLayout时不会发生此问题。虽然此自定义进度条在正常布局中工作正常,但它在TabLayout中不起作用。这有什么特别的原因吗?感谢。



// custom_progress_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <com.wang.avi.AVLoadingIndicatorView
        android:id="@+id/progress_bar"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:visibility="gone"
        app:indicator="BallClipRotatePulse"
        app:indicator_color="@android:color/darker_gray"/>

</RelativeLayout>
&#13;
// One of the fragment of TabLayout

public class FirstFragment extends Fragment {

    private TextView tv1;
    private CShowProgress cShowProgress;
    private static final String CUSTOMERS_INFO = "My API";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.first, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tv1 = (TextView)view.findViewById(R.id.tv1);

        cShowProgress = CShowProgress.getInstance();

        showCustomersDetails();
    }

    private void showCustomersDetails() {

        cShowProgress.showProgress(getActivity());

        StringRequest stringRequest = new StringRequest(Request.Method.POST, CUSTOMERS_INFO,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        cShowProgress.hideProgress();  // THIS DOESNOT WORK AS PROGRESSBAR STILL SHOWS

                        try {

                            JSONObject jsonObject = new JSONObject(response);

                            JSONArray jsonArray = jsonObject.getJSONArray("aboutuser");

                            for(int i=0; i<jsonArray.length(); i++){

                                JSONObject obj = jsonArray.getJSONObject(i);

                                String str1 = "Contact:" + obj.getString("primary_contact");
                                String str2 = "EMail:" + obj.getString("email");
                               String total = str1 + "\n" + str2;
                                tv1.setText(total);
                            }


                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();

                params.put("spaid", "145");
                params.put("customer_id", "64");
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);
    }



}
&#13;
//Custom Class for 3rd party library of progressbar

public class CShowProgress {
    public static CShowProgress s_m_oCShowProgress;
    public static Context m_Context;
    public Dialog m_Dialog;

    public CShowProgress(Context m_Context) {
        this.m_Context = m_Context;
    }

    public static CShowProgress getInstance() {
        if (s_m_oCShowProgress == null) {
            s_m_oCShowProgress = new CShowProgress(m_Context);
        }
        return s_m_oCShowProgress;
    }

    public void showProgress(Context m_Context) {
        m_Dialog = new Dialog(m_Context);
        m_Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        m_Dialog.setContentView(R.layout.custom_progress_layout);
        m_Dialog.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
        m_Dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        m_Dialog.setCancelable(true);
        m_Dialog.setCanceledOnTouchOutside(true);
        m_Dialog.show();
    }

    public void hideProgress() {
        if (m_Dialog != null) {
            m_Dialog.dismiss();
            m_Dialog = null;
        }
    }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

试试这个,

  public void hideProgress() {
    if (m_Dialog != null) {
        m_Dialog.setVisible(View.GONE);
        m_Dialog = null;
    }
}

onResponse()

 cShowProgress.setVisible(View.GONE);

答案 1 :(得分:0)

我在自定义进度对话框类中进行了一些更改,现在它可以正常运行...

&#13;
&#13;
public class CShowProgress extends Dialog{
    private Dialog mpd = null;
    private LayoutInflater inflater = null;

    public CShowProgress(Context context) {
        super(context)
    }

    

    public void showProgress(Context context) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View view = inflater.inflate(R.layout.custom_progress_layout, null);

        mpd = new Dialog(context);
        mpd.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mpd.setContentView(view);
        mpd.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
        mpd.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        mpd.setCancelable(true);
        mpd.show();
    }

    public void hideProgress() {
        if (mpd != null) {
            if (mpd.isShowing()){
                mpd.dismiss();
            }
        }
    }
}
&#13;
&#13;
&#13;