Android ScrollView中嵌套GridView,ListView只显示一行的解决办法

来源:互联网 发布:mac pro 13.3屏幕尺寸 编辑:程序博客网 时间:2024/06/05 11:23

注:本文是由网上大神分享的解决方案汇集而成

方法一:

重写ListView、GridView:

重写ListView:

[java] view plaincopy
  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] view plaincopy
  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. }  
xml中的布局:

[html] view plaincopy
  1. <com.xxx.MyGridView   
  2.              android:id="@+id/mygridview"   
  3.              android:layout_width="fill_parent"   
  4.              android:layout_height="wrap_content"   
  5.              android:gravity="center"   
  6.              android:horizontalSpacing="5dp"   
  7.              android:numColumns="4"   
  8.              android:stretchMode="columnWidth"   
  9.              android:verticalSpacing="6dp" />   
此方案亲测有效。

方法二:通过计算出来ListView或者GridView中的子列高度和 进行显示:

[java] view plaincopy
  1. public void setListViewHeightBasedOnChildren(ListView listView) {    
  2.     ListAdapter listAdapter = listView.getAdapter();     
  3.     if (listAdapter == null) {    
  4.         return;    
  5.     }    
  6.   
  7.     int totalHeight = 0;    
  8.     for (int i = 0; i < listAdapter.getCount(); i++) {    
  9.         View listItem = listAdapter.getView(i, null, listView);    
  10.         listItem.measure(00);    
  11.         totalHeight += listItem.getMeasuredHeight();    
  12.     }    
  13.   
  14.     ViewGroup.LayoutParams params = listView.getLayoutParams();    
  15.     params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));    
  16.     ((MarginLayoutParams)params).setMargins(10101010);  
  17.     listView.setLayoutParams(params);    
  18. }   
此方案,测试过同样可以达到效果
0 0
原创粉丝点击