閱讀564 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ListView設置EmptyView的方法

原文:https://blog.csdn.net/yuanzeyao2008/article/details/8153365

我們在使用ListView展示數據時,如何需要展示的數據集為空,那麼就會顯示一個黑屏,為了解決該問題,ListView有一個方法setEmptyView,當數據集為空時,就顯示設置的這個界麵。

現在分兩種情況來分析這個問題:

如果你的Activity繼承ListActivity:

這種情況相對簡單,

定義非空時的xml

  1. <LinearLayout  
  2.   xmlns:android="https://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent">  
  6.     
  7.     <ListView   
  8.        android:id="@android:id/list"  
  9.        android:layout_width="fill_parent"   
  10.        android:layout_height="fill_parent"   
  11.     ></ListView>  
  12.       
  13.     <ViewStub android:id="@android:id/empty"   
  14.            android:layout_width="fill_parent"   
  15.        android:layout_height="fill_parent"   
  16.             android:layout_gravity="center"   
  17.             android:layout="@layout/emptyview"   
  18.     />          
  19. </LinearLayout>  

定義emptyview.xml

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.     <Button android:id="@+id/btn_emptyview"  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"  
  8.         android:text="EmptyView視圖"  
  9.         android:textSize="20pt"  
  10.         />"  
  11.       
  12.   
  13. </LinearLayout>  


對於這種情況,隻需要這兩個xml就可以完成

 

如果使用普通的Activity完成

定義非空時的xml:

  1. <LinearLayout  
  2.   xmlns:android="https://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent">  
  6.     
  7.     <ListView   
  8.         android:id="@+id/list"  
  9.         android:layout_width="fill_parent"   
  10.        android:layout_height="fill_parent"   
  11.     ></ListView>  
  12.       
  13.     <ViewStub android:id="@+id/empty"   
  14.        android:layout_width="fill_parent"   
  15.        android:layout_height="fill_parent"   
  16.        android:layout_gravity="center"   
  17.        android:layout="@layout/emptyview"   
  18.     />       
  19.       
  20.       
  21. </LinearLayout>  

定義空時的xml和上麵一樣

區別在於Actiivty中的代碼

  1. public class SecondActivity extends Activity  
  2. {  
  3.     //private static final String[]items={"A","N","C"};  
  4.     private static final String[]items={};  
  5.     private ListView list;  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState)  
  8.     {  
  9.         // TODO Auto-generated method stub  
  10.         super.onCreate(savedInstanceState);  
  11.         this.setContentView(R.layout.noempty);  
  12.         ArrayAdapter<String>adaptr=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);  
  13.         list=(ListView)this.findViewById(R.id.list);  
  14.         list.setAdapter(adaptr);  
  15.         ViewStub mViewStub = (ViewStub)findViewById(R.id.empty);    
  16.   
  17.         list.setEmptyView(mViewStub);            
  18.   
  19.     }  
  20. }  

最後更新:2017-04-03 22:31:03

  上一篇:go 分享30個開發人員有用的CSS代碼片段
  下一篇:go 請注釋你那該死的代碼!