简体   繁体   English

Android Studio 中的进度条在延迟时未显示

[英]Progressbar in Android Studio is not showing while delay

I have a method in which I want a delay of a second, and while the delay is running there should be a loading animation ( ProgressBar ).我有一种方法,我想要延迟一秒钟,并且在延迟运行时应该加载 animation ( ProgressBar )。

Right now, when the method is now running the loading animation is not appearing.现在,当该方法正在运行时,加载 animation 没有出现。 If I do not call the Timeout it does appear, and when I do not make it invisible after, it shows up after the timeout.如果我不调用Timeout ,它确实会出现,并且当我没有让它不可见时,它会在超时后出现。

How do I show the loading animation while the timeout is running?如何在超时运行时显示加载 animation? I have a similar problem trying to do with with Thread.sleep(1000) .我尝试与Thread.sleep(1000)做类似的问题。

public void firstMethod(){
    ProgressBar pgLoading = (ProgressBar) findViewById(R.id.pgLoading);
    
    pgLoading.setVisibility(VISIBLE);
    
    try {
        TimeUnit.SECONDS.sleep(1);
    }catch(InterruptionException ex){
        ex.printStackTrace();
    }

    pgLoading.setVisibility(INVISIBLE);
}

By calling the sleep method you are making the UI thread sleep for 1 second during that time nothing will happen on UI Thread hence you do not see the ProgressBar.通过调用 sleep 方法,您可以让 UI 线程在此期间休眠 1 秒钟,UI 线程上不会发生任何事情,因此您看不到 ProgressBar。 You should instead use a TimerTask to wait for this one second and then close the ProgressBar.您应该改为使用TimerTask等待这一秒,然后关闭 ProgressBar。 Checkout this link on how to use TimerTask.查看此链接以了解如何使用 TimerTask。

Main thread can't be blocked.主线程不能被阻塞。 That will cause the entire rendering to stop.这将导致整个渲染停止。 That's why you only see the result after that timeout.这就是为什么您只能在超时后看到结果。

These kind of operations shoud be handled in other threads.这类操作应该在其他线程中处理。 If you are using Java you can use Runnables but you should consider moving to Kotlin to use coroutines.如果您使用的是 Java,您可以使用 Runnables,但您应该考虑迁移到 Kotlin 以使用协程。

EG:例如:

    pgLoading.setVisibility(VISIBLE);
    new Thread() {
        public void run() {
            Thread.sleep(1000);
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                  pgLoading.setVisibility(INVISIBLE);
               }
            });
           }
          }
        }.start();

This is happening because the current thread is being paused.To avoid this put your delay/long process in a different thread.For example:发生这种情况是因为当前线程正在暂停。为避免这种情况,请将您的延迟/长时间进程放在不同的线程中。例如:

public void firstMethod(){
            ProgressBar pgLoading = (ProgressBar) findViewById(R.id.pgLoading);

            pgLoading.setVisibility(View.VISIBLE);
new Thread(()->{
//    Do all your long process here
    try {
        TimeUnit.SECONDS.sleep(1);
    }catch( InterruptedException ex){
        ex.printStackTrace();
    }
    runOnUiThread(() -> pgLoading.setVisibility(View.INVISIBLE));

}).start();




    }

You can use delay您可以使用延迟

import android.os.Handler;

 
public void firstMethod()
{


//Initialize and make progress **strong text**bar visible here

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
    // Do something after 1s = 1000ms
      //Make your progress bar invisible 

 }
 }, 1000);
 }        

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

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