ScrollView嵌套ListView只显示一行

来源:互联网 发布:淘宝如何改会员名 编辑:程序博客网 时间:2024/06/05 18:18

使用网上用的动态改变listview高度的方法,该方法只适用于item布局是LinearLayout布局的情况,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。所以使用限制较大。

在开发的过程当中,由于手机屏幕的大小的限制,我们经常需要使用滑动的方式,来显示更多的内容。在最近的工作中,遇见一个需求,需要将ListView嵌套到ScrollView中显示。于是乎有了如下布局: 

 

  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的列表项计算列表的尺寸。实现代码如下: 

 

  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问题搞定,打完收工 

 


http://www.cnblogs.com/zhwl/p/3333585.html



或者

.重写ListView、gridView(推荐):

    重写ListView

Java代码  收藏代码
  1. public class MyListView extends ListView {  
  2.   
  3.     public MyListView(Context context) {  
  4.         // TODO Auto-generated method stub  
  5.         super(context);  
  6.     }  
  7.   
  8.     public MyListView(Context context, AttributeSet attrs) {  
  9.         // TODO Auto-generated method stub  
  10.         super(context, attrs);  
  11.     }  
  12.   
  13.     public MyListView(Context context, AttributeSet attrs, int defStyle) {  
  14.         // TODO Auto-generated method stub  
  15.         super(context, attrs, defStyle);  
  16.     }  
  17.   
  18.     @Override  
  19.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  20.         // TODO Auto-generated method stub  
  21.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  22.                 MeasureSpec.AT_MOST);  
  23.         super.onMeasure(widthMeasureSpec, expandSpec);  
  24.     }  
  25. }  
 

   同样适用与重写GridView

 

Java代码  收藏代码
  1. /** 
  2.  * 自定义gridview,解决ListView中嵌套gridview显示不正常的问题(1行半) 
  3.  * @author wangyx 
  4.  * @version 1.0.0 2012-9-14 
  5.  */  
  6. public class MyGridView extends GridView{  
  7.       public MyGridView(Context context, AttributeSet attrs) {   
  8.             super(context, attrs);   
  9.         }   
  10.        
  11.         public MyGridView(Context context) {   
  12.             super(context);   
  13.         }   
  14.        
  15.         public MyGridView(Context context, AttributeSet attrs, int defStyle) {   
  16.             super(context, attrs, defStyle);   
  17.         }   
  18.        
  19.         @Override   
  20.         public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   
  21.        
  22.             int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,   
  23.                     MeasureSpec.AT_MOST);   
  24.             super.onMeasure(widthMeasureSpec, expandSpec);   
  25.         }   
  26. }  
 

0 0
原创粉丝点击