【问题标题】:Android Progress Bar not appearing安卓进度条没有出现
【发布时间】:2018-08-30 11:21:49
【问题描述】:

我已尝试按照此处有关较旧问题的建议来实现异步任务,以便在方法运行时显示我的进度条(该方法大约需要 5 秒才能完成)

这里是异步任务的实现:

public void runTask() {

        new YourAsyncTask().execute();
    }

    private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... args) {
            tableStatus();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Loading.setVisibility(View.GONE);
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Loading.setVisibility(View.VISIBLE);
        }
    }

表格状态方法为:

public void tableStatus(){

        String tables[] = {"1", "2", "3", "4", "5", "6", "7", "8"};
        for(int i=0; i<tables.length; i++){
            availableTables(tables[i]);
        }

    }

而availableTables方法是一个StringRequest方法。

有人对如何解决这个问题有任何建议吗?

【问题讨论】:

  • 好吧,Volley 的StringRequest 已经用于异步执行,您不必在循环后立即从AsyncTask. What happning is doInBackground 调用它。为什么在循环中发出请求会找到其他方式这是你的要求。可能需要修改 API。
  • Volley 和 Async 一起是没用的。使用 volley 本身,您可以进行网络调用,那么为什么要使用 Async。而且,顺便说一句,上面的实现也不对。
  • 显示你的 layout.xml 代码。

标签: android progress-bar


【解决方案1】:

代码对我来说似乎很好,但如果不工作,显然有问题。

我正在使用的代码可能对您有所帮助,它是一个线性布局内的 ProgressBar 和一个 TextView,所以首先我得到了 LinearLayout:

LinearLayout progressBarLinearLayout = (LinearLayout) findViewById(R.id.progressBar);
progressBarLinearLayout.setVisibility(View.GONE);

然后根据你的代码调整它,应该是这样的:

public void runTask() {

        new YourAsyncTask().execute();
    }

    private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... args) {
            tableStatus();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            progressBarLinearLayout.setVisibility(View.GONE);
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBarLinearLayout.setVisibility(View.VISIBLE);
        }
    }

我希望这也适用于你。

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 2015-04-17
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2012-06-17
    • 2013-12-21
    相关资源
    最近更新 更多