0

我将循环不确定的progressBar 变为可见,然后运行Thread,这可能需要几秒钟(我尝试了AsyncTask,但它不起作用,可能是因为在该方法中运行了另一个AsyncTask)。但是,progressBar 仅在线程完成后才可见。如何在运行线程之前使其可见,以便用户可以看到正在发生的事情?

编辑:代码相当复杂,所以不可能在没有长描述的情况下全部附上,但是,我会尝试附上代码的重要部分

  //this is the Thread class
  private static final class LoadingMolecules extends Thread{
        private Intent intent;
        private ArrayList<Integer> groups;
        Context context;
        QuizProvider quizProvider;

    public LoadingMolecules (Intent intent,ArrayList<Integer> groups, Context context ){
        this.intent = intent;
        this.groups = groups;
        this.context = context;
    }

    @Override
    public void run() {
        quizProvider = new QuizProvider(groups, context);
        //inside next method is the asyncTask
        quizProvider.loadAllCompounds();
    }


}


//there is the code running
progressBar.setVisibility(View.VISIBLE);
        LoadingMolecules lm = new LoadingMolecules(intent, groups, this);
        lm.start();
        try {
            lm.join();
        }catch (InterruptedException ignore){

        }
        intent.putExtra("quizProvider", lm.quizProvider);
        startActivity(intent);
        finish();
4

1 回答 1

0

你应该:

  1. 可见进度。
  2. 启动后台线程。
  3. 隐藏ui线程中的进度。

这样:

progress.visibility = View.VISIBLE
AppController.getInstance().getAppExecutors().diskIO().execute(() -> {
   //do stuff
    runOnUiThread(() -> {
         //do stuff
         progress.visibility = View.GONE
    });

});
于 2020-04-02T17:32:19.720 回答