2012-12-04 9 views
5

Mam widok z EditText, Button i ListView. Guzik onClick() analizuje pewną witrynę (ta część działa poprawnie), ale wyświetlenie przeanalizowanych informacji w ListView zajmuje trochę czasu, dlatego chcę wyświetlić małą ProgressBar w miejscu, gdzie po pewnym czasie musi nastąpić ListView.Jak wyświetlić pasek postępu w widoku podczas wykonywania pracy?

Więc, dodaję to do mojego układu (piszę ważną rolę tutaj):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1" 
     android:weightSum="1.0"> 

     <EditText 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight=".86" /> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0px" 
      android:layout_weight=".14" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_below="@id/layout1" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     android:id="@+id/progressbar" > 

     <ProgressBar 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      style="@android:style/Widget.ProgressBar.Small" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/layout1"> 

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

    </RelativeLayout> 

</RelativeLayout> 

W kodzie źródłowym:

progressBar = (RelativeLayout) findViewById(R.id.progressbar); 

i za każdym razem chcę pokazać ProgressBar, mogę to zrobić :

progressBar.setVisibility(View.VISIBLE); 

wyłączania:

progressBar.setVisibility(View.INVISIBLE); 

Ale to nie działa.

Jak mogę to załatwić?

+0

* Ale to nie działa. * - to nie jest bardzo pomocne. Chcesz lepiej wyjaśnić, na czym polega problem? – Luksprog

+0

Nie widzę żadnego wyświetlanego paska "ProgressBar". Działa tak, jakby działało bez dodawania drobiazgów "ProgressBar". – azizbekian

+2

Zgadzam się z Luksprogiem, ale byłbym skłonny zgadnąć, że twoje parsowanie jest blokowane (więc kody setvisibility nie są wykonywane aż do postsese). Istnieje mnóstwo sposobów rozwiązania tego problemu, ale polecam wykonanie wyszukiwania pasków postępu i asynktaksu (są też inne sposoby, aby to osiągnąć, ale zasadniczo musisz uruchomić niezablokowaną metodę displayprogressbar w tym samym czasie, gdy robisz parsowanie, a następnie w displayprogressbar sprawdź przed zakończeniem procesu parsowania). – logray

Odpowiedz

2

Nie ma potrzeby, aby manipulować swoją widoczność ProgressBar „s. ListView ma funkcję o nazwie "pusty widok", która jest wyświetlana tylko wtedy, gdy numer ListView jest pusty. Oto, co trzeba zrobić, aby go użyć:

  1. Jeśli aktywność rozciąga ListActivity użyć android:id="@android:id/list" Dla Państwa ListView i android:id="@android:id/empty" dla ProgressBar.
  2. Jeśli nie przedłużysz ListActivity, jest to setEmptyView() metoda Twojego ListView, która pozwala zrobić to samo.

Powyższa metoda najlepiej pasuje, jeśli nie trzeba opróżniać swojego ListView (np. Dane są ładowane tylko jeden raz podczas uruchamiania czynności). Jeśli chcesz ustawić dane po tym, lepiej jest użyć ViewSwitcher, co również ma wielką zaletę - pozwala zastosować animację przejścia. Aby uzyskać szczegółowe informacje na temat ViewSwitcher, spójrz na this.

0

Spróbuj użyć asynchronicznego zadania, jak wspomniano powyżej.

/** 
* this class performs all the work, shows dialog before the work and dismiss it after 
*/ 
public class ProgressTask extends AsyncTask<String, Void, Boolean> { 

public ProgressTask(ListActivity activity) { 
    this.activity = activity; 
    dialog = new ProgressDialog(context); 
} 

/** progress dialog to show user that the backup is processing. */ 
private ProgressDialog dialog; 
/** application context. */ 
private ListActivity activity; 

protected void onPreExecute() { 
    this.dialog.setMessage("Progress start"); 
    this.dialog.show(); 
} 

    @Override 
protected void onPostExecute(final Boolean success) { 
    if (dialog.isShowing()) { 
     dialog.dismiss(); 
    } 


    //do whatever with your data 
} 

protected Boolean doInBackground(final String... args) { 
    try{  
     //parsing of your website here... 

     return true; 
    } catch (Exception e) 
     Log.e("tag", "error", e); 
     return false; 
    } 
    } 
} 
+0

Używanie modalnych okien dialogowych ma jedną wadę - blokują całe okno. Użytkownik może kontynuować interakcję z aplikacją podczas ładowania. –

1

RelativeLayout z ListView ukrywa swoją ProgressBar .Modify układ jak ten i zobaczyć jak to działa:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1" 
     android:weightSum="1.0"> 

     <EditText 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight=".86" /> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0px" 
      android:layout_weight=".14" /> 

    </LinearLayout> 

    <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_below="@id/layout1"/> 

    <RelativeLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     android:id="@+id/progressbar" > 

     <ProgressBar 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      style="@android:style/Widget.ProgressBar.Small" /> 

    </RelativeLayout> 


</RelativeLayout> 

Również spojrzeć at one of my answers, dla nieco podobny problem.

1

Nie musisz zagnieżdżać swojego ProgressBar w innym RelativeLayout. Po prostu wyśrodkuj go w głównym układzie i umieść go na końcu, aby pozostał na górze.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    // the rest 

    <ProgressBar 
     android:id="@+id/progressBar" 
     android:layout_centerInParent="true" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

</RelativeLayout> 
0

Dla tych, którzy są zainteresowani odpowiedzią.

Główne znaczniki są pisane.

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1" 
     android:weightSum="1.0" > 

     <EditText 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight=".86" /> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0px" 
      android:layout_weight=".14" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/layout1" > 

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/layout1" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/layout1" 
     android:gravity="center" 
     android:visibility="gone" 
     android:background="#DDE7E7E8" 
     android:id="@+id/pBar" > 

     <ProgressBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@android:style/Widget.ProgressBar.Small" /> 

    </LinearLayout> 

</RelativeLayout> 

A AsyncTask:

private class Parsing extends AsyncTask<Void, Void, Void> 
{ 
    private LinearLayout pBar; 

    private Parsing() 
    { 
     pBar = (LinearLayout) findViewById(R.id.pBar); 
    } 

    @Override 
    protected Void doInBackground(Void... params) 
    { 
     parsingHere(); 
     return null; 
    } 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
     pBar.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected void onPostExecute(Void result) 
    { 
     super.onPostExecute(result); 
     pBar.setVisibility(View.INVISIBLE); 
    } 

}