简体   繁体   English

Android进度对话框微调器未显示

[英]Android Progress Dialog Spinner not showing

I have created a class for Progress Dialog just to avoid rewriting few line of code. 我为“进度对话框”创建了一个类,只是为了避免重写几行代码。 Please check the code below. 请检查下面的代码。

public class AppProgressDialog {

/**
 * progress dialog object 
 */
private ProgressDialog progressDialog;

/**
 * stores is progress dialog is cancellable 
 */
private boolean isProgressDialogCancelable = false;

/**
 * style of progress dialog
 */
private int progressDialogStyle = ProgressDialog.STYLE_SPINNER;

/**
 * true if progress time not known
 */
private boolean isIndetermined = true;

/**
 * constructor to initialize this custom progress dialog class
 * @param activity activity to which this progress dialog will belong
 * @param message Message to be shown inside progress dialog
 */
public AppProgressDialog(Activity activity) {
    progressDialog = new ProgressDialog(activity);
    progressDialog.setCancelable(isProgressDialogCancelable);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setIndeterminate(isIndetermined);
}

/**
 * function to set display message inside progress dialog
 * @param message string to be shown inside progress dialog 
 */
public void setProgressDialogMessage(String message)
{
    if (progressDialog!=null) {
        progressDialog.setMessage(message);
    }
}

/**
 * function to show progress dialog
 */
public void showProgressDialog()
{
    if (progressDialog!=null) {
        if(!progressDialog.isShowing())
            progressDialog.show();
    }
}

/**
 * function to hide progress dialog
 */
public void dismissProgressDialog()
{
    if (progressDialog!=null) {
        if(progressDialog.isShowing())
            progressDialog.hide();
    }
}

And this is how I am using it in a Fragment: 这就是我在片段中使用它的方式:

public class TestFragment extends Fragment {

private AppProgressDialog progressDialog;

public static TestFragment newInstance() {

    TestFragment fragment = new TestFragment();
    return fragment;

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    progressDialog = new AppProgressDialog(getActivity());
}

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_login, container, false);

    Button btnTest = rootView.findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new TestAsyncTask().execute();

        }
    });
    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);
}

@Override
public void onResume() {
    super.onResume();
}

class TestAsyncTask extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        progressDialog.setProgressDialogMessage("Logging you to eventizon");
        progressDialog.showProgressDialog();
    }

    @Override
    protected String doInBackground(String... params) {
        //some code
    }

    @Override
    protected void onPostExecute(String result) {
        progressDialog.dismissProgressDialog();
    }

}

} }

Now, the problem is I am not able to see the spinner which is usually seen in a Progress Dialog. 现在,问题是我看不到通常在“进度”对话框中看到的微调器。 I am testing this on Lollipop. 我正在棒棒糖上对此进行测试。

You need to run the progress dialog in UI thread,So in your AsyncTask preExecute method do like below, 您需要在UI线程中运行进度对话框,因此在您的AsyncTask preExecute方法中执行以下操作,

getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    progressDialog = new AppProgressDialog(getActivity());
                    progressDialog.setProgressDialogMessage("Logging you to eventizon");
                    progressDialog.showProgressDialog();
                    }
                });

and onPostExecute dismiss the dialog. 然后onPostExecute关闭对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM