1

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

在与服务器通信时,我显示了一个进度条,如下所示:

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

并像这样隐藏它:

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

按照此处的建议完成后。

它工作得非常好。

现在大多数时候,对服务器的请求只需要几毫秒,因此显示和隐藏进度条会导致一些闪烁,如果请求时间很短,这是不必要的和烦人的。

什么是仅在请求开始后一定时间后才显示进度条的好方法,比如说在一秒钟后?

4

4 回答 4

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
        }
    });
}
于 2019-03-14T16:59:36.300 回答
0

您可以简单地CountDownTimer在第二个刻度中使用 a。像这样:

 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);
于 2019-03-14T17:10:15.240 回答
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);
于 2019-03-14T16:51:20.047 回答
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);
于 2019-03-14T17:04:37.260 回答