日積月累:ScrollView嵌套ListView只顯示一行


在開發的過程當中,由於手機屏幕的大小的限制,我們經常需要使用滑動的方式,來顯示更多的內容。在最近的工作中,遇見一個需求,需要將ListView嵌套到ScrollView中顯示。於是乎有了如下布局: 

 

[html]   view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.     xmlns:tools="http://schemas.android.com/tools"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:background="#FFE1FF"   
  6.     android:orientation="vertical" >   
  7.     <ScrollView   
  8.         android:layout_width="match_parent"   
  9.         android:layout_height="match_parent" >   
  10.         <LinearLayout   
  11.             android:layout_width="match_parent"   
  12.             android:layout_height="match_parent" >   
  13.             <ListView   
  14.                 android:id="@+id/listView1"   
  15.                 android:layout_width="match_parent"   
  16.                 android:layout_height="match_parent"   
  17.                 android:fadingEdge="vertical"   
  18.                 android:fadingEdgeLength="5dp" />   
  19.         </LinearLayout>   
  20.     </ScrollView>   
  21. </LinearLayout>   

 

運行程序,如下結果,無論你如何調整layout_width,layout_height屬性,ListView列表只顯示一列 

在查閱的各種文檔和資料后,發現在ScrollView中嵌套ListView空間,無法正確的計算ListView的大小,故可以通過代碼,根據當前的ListView的列表項計算列表的尺寸。實現代碼如下: 

 

[java]   view plain copy
  1. public class MainActivity extends Activity {   
  2.     private ListView listView;   
  3.     @Override   
  4.     protected void onCreate(Bundle savedInstanceState) {   
  5.         super.onCreate(savedInstanceState);   
  6.         setContentView(R.layout.activity_main);   
  7.         listView = (ListView) findViewById(R.id.listView1);   
  8.         String[] adapterData = new String[] { "Afghanistan""Albania",… … "Bosnia"};   
  9.         listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));   
  10.         setListViewHeightBasedOnChildren(listView);   
  11.     }   
  12.     public void setListViewHeightBasedOnChildren(ListView listView) {   
  13.         // 獲取ListView對應的Adapter   
  14.         ListAdapter listAdapter = listView.getAdapter();   
  15.         if (listAdapter == null) {   
  16.             return;   
  17.         }   
  18.    
  19.         int totalHeight = 0;   
  20.         for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   
  21.             // listAdapter.getCount()返回數據項的數目   
  22.             View listItem = listAdapter.getView(i, null, listView);   
  23.             // 計算子項View 的寬高   
  24.             listItem.measure(00);    
  25.             // 統計所有子項的總高度   
  26.             totalHeight += listItem.getMeasuredHeight();    
  27.         }   
  28.    
  29.         ViewGroup.LayoutParams params = listView.getLayoutParams();   
  30.         params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));   
  31.         // listView.getDividerHeight()獲取子項間分隔符占用的高度   
  32.         // params.height最后得到整個ListView完整顯示需要的高度   
  33.         listView.setLayoutParams(params);   
  34.     }   
  35. }   
運行結果,OK問題搞定,打完收工  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM