如何在一定时间后显示 ProgressBar
How to show ProgressBar after a certain amount of time
我正在使用 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。
它运行得非常好。
现在大多数时候,对服务器的请求只需要几毫秒,所以显示和隐藏进度条会导致一些闪烁,如果请求时间只有那么短,这是不必要的而且很烦人。
仅在请求开始后一定时间后才显示进度条的好方法是什么,比方说一秒后?
您可以像这样在显示进度条之前设置延迟:
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);
我猜你做了类似下面的事情
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
}
});
}
您可以使用最初设置为 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);
您可以简单地在第二个滴答中使用 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);
我正在使用 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。
它运行得非常好。
现在大多数时候,对服务器的请求只需要几毫秒,所以显示和隐藏进度条会导致一些闪烁,如果请求时间只有那么短,这是不必要的而且很烦人。
仅在请求开始后一定时间后才显示进度条的好方法是什么,比方说一秒后?
您可以像这样在显示进度条之前设置延迟:
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);
我猜你做了类似下面的事情
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
}
});
}
您可以使用最初设置为 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);
您可以简单地在第二个滴答中使用 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);