简体   繁体   English

我如何在异步任务类中使用循环旋转进度条

[英]How i can use circular spinning progress bar in my async task class

I want to use spinning progress bar in asynctask class. 我想在asynctask类中使用旋转进度条。 Below is my asynctask class code. 下面是我的asynctask类代码。

Before i use progress dialog which works properly. 在使用进度对话框之前,它可以正常工作。

public class Downloader extends AsyncTask<Void,Void,Object> {


    Context c;
    String urlAddress;
    RecyclerView rv;
public Downloader(Context c, String urlAddress, RecyclerView rv) {
    this.c = c;
    this.urlAddress = urlAddress;
    this.rv = rv;
    }


@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Object doInBackground(Void... voids) {
    return this.downloadData();
}

@Override
protected void onPostExecute(Object data) {
    super.onPostExecute(data);

    if(data.toString().startsWith("Error"))
    {
        Toast.makeText(c,data.toString(),Toast.LENGTH_SHORT).show();
    }else new RssParser(c, (InputStream) data, rv).execute();
}

private Object downloadData()
{
    Object connection=Connector.connect(urlAddress);
    if(connection.toString().startsWith("Error"))
    {
        return connection.toString();
    }

Please comment if you want any other information. 如果您需要其他任何信息,请发表评论。

If you want any customization for showing the progress bar in asycTask you can refer following link. 如果您想要进行任何自定义以在asycTask中显示进度条,则可以参考以下链接。 Android custom progress indicator while doing AsyncTask 执行AsyncTask时,Android自定义进度指示器

For my projects somtimes I use this method: 对于我的项目,我有时使用这种方法:

I attached ProgressBar into my layout, but I did it invisible. 我将ProgressBar附加到布局中,但看不到它。 And in code I write this steps: 在代码中,我编写了以下步骤:

private class ParseTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {

       ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setVisibility(ProgressBar.VISIBLE);

            //My code
            }




@Override
protected void onPostExecute(String strJson) {

    //My code

    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(ProgressBar.INVISIBLE);



}

}

So, I "turn on" ProgressBar in doInBackground and "turn off" it in onPostExecute 所以,我在“打开”进度doInBackground和“关闭”它onPostExecute

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

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