2012-12-02 38 views
0

我有一個列表視圖的活動,它顯示我從在線獲取的一些數據。在顯示列表視圖之前,我有一箇中間的進度指示器。該活動以隱藏的列表視圖和顯示的進度指示器開始,並且在必要時隱藏一個並顯示另一個。他們在相互之間的框架佈局中。現在我想添加顯示帶有錯誤消息而不是列表視圖的文本視圖的可能性(如有必要)。有沒有更清晰的方法比隱藏兩個視圖,並顯示所需的每次我想改變它?Android根據狀態顯示不同的視圖

+0

如果你想顯示錯誤消息只考慮使用吐司消息 –

回答

0

,而不是我發現了一個不錯的解決方案。建議使用ViewSwitcher,而不是ViewFlipper,它允許多個視圖。此外,它可以讓你對

<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/viewFlipper" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" /> 

</ViewFlipper> 

視圖之間切換視圖之間切換設置動畫是一個有點亂,雖然

mViewFlipper.setDisplayedChild(mViewFlipper.indexOfChild(findViewById(R.id.myView)));

也許可以使用硬編碼的指標太多,如果不會有太多的孩子意見。

0

您可以使用ViewSwitcher。您可以在其中添加一些視圖並按順序顯示它們,調用showNext()

+0

這似乎只允許兩個子視圖 – dyancat

0

您可以隨時調用AdapterView對象上的AdapterView.setEmptyView(View v),以將您希望用作視圖的視圖添加爲空視圖。

的片段如下:

empty = (TextView)findViewById(R.id.empty1); 
list = (ListView)findViewById(R.id.list1); 
list.setEmptyView(empty); 

請務必保持同一母公司內部的ListView和TextView的。

有關詳細說明,請參閱this

+0

不正是我一直在尋找,但它確實幫助我與我的問題一個又一個,謝謝了! – dyancat

1

這是我如何定義我的XML,它包含有一個進度條ListView

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

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

      <ProgressBar 
       android:id="@+id/pbHeaderProgress" 
       style="@style/Spinner" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="2dp" > 
      </ProgressBar> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:gravity="left|center" 
       android:padding="2dp" 
       android:text="Loading...." 
       android:textColor="#F1F1F1" 
       android:textSize="20sp" > 
      </TextView> 
     </LinearLayout> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:cacheColorHint="@android:color/transparent" 
      android:divider="#000000" 
      android:dividerHeight="0dp" 
      android:fadingEdge="none" 
      android:persistentDrawingCache="scrolling" > 
     </ListView> 

     <TextView 
      android:id="@android:id/empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:padding="10dp" 
      android:textColor="#f1f1f1" 
      android:textSize="15sp" 
      android:textStyle="bold" 
      android:visibility="gone" > 
     </TextView> 
    </LinearLayout> 

</LinearLayout> 

在這種用於提取和顯示數據(在我的情況下,Facebook的API),在一個AsyncTaskActivity,我檢查數據在JSONArray返回的長度爲 。

例如:

TextView txtEmptyData = (TextView) findViewById(android.R.id.empty); 
if (JAFeeds.length() == 0) { 
    txtEmptyData.setVisibility(View.VISIBLE); 
else { 
    txtEmptyData.setVisibility(View.GONE); 

    // DO THE REST OF THE PROCESSING TO FETCH THE REMAINING DATA TO DISPLAY 
} 

,因爲XML的定義方式,如果length() == 0,那麼TextView變得可見,並得到顯示的ListView

+0

嗯,我可以看到這將如何工作,但它感覺有點hackish。我只是一個新手到android,所以我不知道哈哈。我發現了另一個我發佈的解決方案 – dyancat

+0

@dvdy:很高興你找到了解決方案。 :-)。而且,這不是一個黑客;-) – SSL

相關問題