我正在使进度条在异步任务中可见和不可见。我正在异步任务中执行一些数据库操作,同时执行我想显示进度对话框的操作。一旦它完成操作。我想让它看不见。我的代码如下。问题是进度条不显示。
activity_xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_course_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.lms.CourseList">
<!-- The ActionBar displayed at the top -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/course_list"></ListView>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
我正在使用异步任务,我想让进度条可见和不可见
private class tmpCourseOfferingTask extends AsyncTask<String,Void,JSONObject> {
public tmpCourseOfferingTask(){
super();
}
@Override
protected void onPreExecute(){
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected JSONObject doInBackground(String...params){
return null;
}
@Override
protected void onPostExecute(JSONObject json) {
Thread.sleep(5000);
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.setVisibility(View.GONE);
}
最佳答案
首先,从onPostExecute中删除Thread.Sleep(millis)并将其放入doInBackground中。 onPostExecute 和 onPreExecute 一样,在主线程上运行,因此 sleep 应该放在后台进程中。
您的流程(目前)是这样的:
asynctask
已创建预执行
=进度条
可见onbackground
= 无执行后
=线程 sleep
。 --> 这是停止 UI 线程, View 没有时间更新可见性,因为它已停止进度条
=View.GONE
。
这没有显示进度,因为线程立即停止,并且 View 更改在返回后应用一次。我使用如下代码,它对我有用。
另一件事是,您的捕获没有尝试(?),因此此代码缺少某些内容或无法正常工作。通过这两个修复,它就可以工作了。做类似的事情:
private class tmpCourseOfferingTask extends AsyncTask<String,Void,JSONObject> {
public tmpCourseOfferingTask(){
super();
}
@Override
protected void onPreExecute(){
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected JSONObject doInBackground(String...params){
try{
SystemClock.sleep(timeInMills);
}catch(Exception ignored){
}finally{
return null;
}
}
@Override
protected void onPostExecute(JSONObject json) {
progressBar.setVisibility(View.GONE);
}
}
另一个修复:
您有一个带有 LinearLayout 和进度条的RelativeLayout。进度条也应该具有可见性“GONE”,这样 View 空间就不会被使用。您可以删除无用的 LinearLayout 并执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_course_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.lms.CourseList">
<include
amdrpod:id="@+id/includeId"
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_below="@+id/includeId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/course_list"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true" />
</RelativeLayout>
(手写,可能不完美)
关于java - 当java代码中的可见性更改为可见时,进度条不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46175991/