簡體   English   中英

在Java代碼中將可見性更改為可見時,進度條不顯示

[英]Progress Bar is not showing up when visibility is changed to visible in java code

我在異步任務中使進度條可見和不可見。 我在執行異步操作時正在執行一些數據庫操作,而我想顯示進度對話框。 一旦完成操作。 我想使其不可見。 我的代碼如下。 問題是無法顯示進度欄。

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一樣,在主線程上運行,因此應將睡眠置於后台進程中。

您的流程(目前)是這樣的:

  • 創建asynctask
  • pre-execute = progressbar可見
  • onbackground =無
  • post-execute = thread sleep ->這正在停止UI線程 ,並且視圖沒有時間更新可見性,因為它已停止
  • progressbar = View.GONE

這沒有顯示進度,因為線程正在立即停止,並且在返回后將應用一次視圖更改。 我使用下面的代碼,它對我有用。

另一件事是您的捕獲沒有嘗試(?),因此此代碼缺少某些內容或無法正常工作。 有了這兩個修復程序,它將起作用。 做類似的事情:

 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”,這樣就不會保留視圖空間。 您可以刪除無用的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>

(用手工寫,可能並不完美)

要確保看到ProgressBar,請在bringToFront()處使用bringToFront() ,如下所示:

progressBar.bringToFront();

順便說一句,我認為皮爾的答案應該可以解決問題。

嘗試將進度條包裹在如下的線性布局中,然后檢查顯示和隱藏功能,並使用線性布局ID進行顯示和隱藏。

 <LinearLayout
    android:id="@+id/Progress"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone" >

   <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" />
</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM