一定时间后如何显示ProgressBar

时间:2019-03-14 16:40:10

标签: android android-progressbar android-threading

我正在使用Android API级别23(Android 6)。 我正在使用com.loopj.android.http.AsyncHttpClient来实现与后端服务器的异步通信,并且工作正常。

与服务器通信时,我正在显示进度条 像这样:

findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);

并这样隐藏它:

findViewById(R.id.loadingPanel).setVisibility(View.GONE);

suggested here完成时。

它运行得非常好。

现在大多数时候,对服务器的请求仅花费几毫秒,因此显示和隐藏进度条会引起一些闪烁,这是不必要的,并且如果请求时间如此之短,也会很烦人。

只有在请求启动后一定时间(例如,一秒钟后)显示进度条的一种好方法?

4 个答案:

答案 0 :(得分:1)

我猜你做过类似的事情

 AsyncHttpClient client = new AsyncHttpClient();
 client.get("https://www.example.com", new AsyncHttpResponseHandler() {

    @Override
    public void onStart() {
        // called before request is started
       findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        // called when response HTTP status is "200 OK"
        findViewById(R.id.loadingPanel).setVisibility(View.GONE);
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
        // called when response HTTP status is "4XX" (eg. 401, 403, 404)
    }

    @Override
    public void onRetry(int retryNo) {
        // called when request is retried
    }
});

只需尝试以下操作

boolean showProgress= true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AsyncHttpClient client = new AsyncHttpClient();
    client.get("https://www.google.com", new AsyncHttpResponseHandler() {

        @Override
        public void onStart() {
            // called before request is started
            //findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(showProgress){
                        findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
                    }
                }
            }, 5*1000); // your progress will start after 5 seconds
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
            // called when response HTTP status is "200 OK"
            //findViewById(R.id.loadingPanel).setVisibility(View.GONE);
            showProgress = false;
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
            showProgress = false;
        }

        @Override
        public void onRetry(int retryNo) {
            // called when request is retried
        }
    });
}

答案 1 :(得分:0)

您可以设置延迟,然后再显示进度条:

Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something after 1s = 1000ms
        findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
    }
}, 1000);

答案 2 :(得分:0)

您可以使用最初设置为true的布尔标志变量。然后,当您收到服务器的响应时,可以翻转标志值。然后,您可以使用以下处理程序:

Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if(flag)
            findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);            
    }
}, 1000);

Handler endHandler = new Handler();
    endHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
                findViewById(R.id.loadingPanel).setVisibility(View.GONE);            
        }
    }, MAX_WAIT_TIME_YOU_WANT);

答案 3 :(得分:0)

您只需在第二刻中使用CountDownTimer。像这样:

 private boolean mNeedToCloseProgress = false;
 // in second with 100 ms tick
 CountDownTimer mCdtStartProgress = new CountDownTimer(1000, 1000) {

     public void onTick(long millisUntilFinished) {}

     public void onFinish() {
        mNeedToCloseProgress = true;
        findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
     }
  };

然后,当您启动api调用时,您需要使用以下命令启动计时器:

mCdtStartProgress.start();

这将修改标志mNeedToCloseProgress,该标志告诉进度是否需要关闭。

然后,当您完成调用api并使用以下方法获取结果时,取消计时器:

mCdtStartProgress.cancel();
if(mNeedToCloseProgress) findViewById(R.id.loadingPanel).setVisibility(View.GONE);