首页 > 代码库 > android ScrollView中嵌套GridView,ListView只显示一行的解决办法

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

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

方法一:

重写ListView、GridView:

重写ListView:

 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:

 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中的布局:

<com.xxx.MyGridView 
             android:id="@+id/mygridview" 
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content" 
             android:gravity="center" 
             android:horizontalSpacing="5dp" 
             android:numColumns="4" 
             android:stretchMode="columnWidth" 
             android:verticalSpacing="6dp" /> 

此方案亲测有效。

 

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

public void setListViewHeightBasedOnChildren(ListView listView) {  
    ListAdapter listAdapter = listView.getAdapter();   
    if (listAdapter == null) {  
        return;  
    }  

    int totalHeight = 0;  
    for (int i = 0; i < listAdapter.getCount(); i++) {  
        View listItem = listAdapter.getView(i, null, listView);  
        listItem.measure(0, 0);  
        totalHeight += listItem.getMeasuredHeight();  
    }  

    ViewGroup.LayoutParams params = listView.getLayoutParams();  
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
    ((MarginLayoutParams)params).setMargins(10, 10, 10, 10);
    listView.setLayoutParams(params);  
} 

此方案,测试过同样可以达到效果。