ScrollView--嵌套GridView的解决办法

前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全。 

解决办法,自定义一个GridView控件 

[java] view plaincopy
 
  1. public class MyGridView extends GridView {   
  2.   
  3.     public MyGridView(Context context, AttributeSet attrs) {   
  4.         super(context, attrs);   
  5.     }   
  6.   
  7.     public MyGridView(Context context) {   
  8.         super(context);   
  9.     }   
  10.   
  11.     public MyGridView(Context context, AttributeSet attrs, int defStyle) {   
  12.         super(context, attrs, defStyle);   
  13.     }   
  14.   
  15.     @Override   
  16.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   
  17.   
  18.         int expandSpec = MeasureSpec.makeMeasureSpec(   
  19.                 Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);   
  20.         super.onMeasure(widthMeasureSpec, expandSpec);   
  21.     }   
  22. }   


该自定义控件只是重写了GridView的onMeasure方法,使其不会出现滚动条,ScrollView嵌套ListView也是同样的道理,不再赘述。 

XML布局代码 

[html] view plaincopy
 
  1. <ScrollView Android:layout_height="wrap_content"   
  2.         Android:layout_width="fill_parent" android:id="@+id/scroll_content">   
  3.         <com.yourclass.MyGridView xmlns:Android="http://schemas.android.com/apk/res/android"   
  4.             Android:id="@+id/grid_view" android:layout_width="fill_parent"   
  5.             Android:layout_height="wrap_content" android:numColumns="auto_fit"   
  6.             Android:horizontalSpacing="1dip" android:verticalSpacing="1dip"   
  7.             Android:columnWidth="150dip" android:stretchMode="columnWidth"   
  8.             Android:gravity="center">   
  9.                
  10.         </com.yourclass.MyGridView>   
  11.     </ScrollView>   


Java调用代码 

[java] view plaincopy
 
  1. MyGridView gridview = (MyGridView) findViewById(R.id.grid_view);   
  2. gridview.setAdapter(new ImageAdapter(this));  

========================

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

Java代码  收藏代码
  1. public class Utility {  
  2. public static void setListViewHeightBasedOnChildren(ListView listView) {  
  3. //获取ListView对应的Adapter  
  4. ListAdapter listAdapter = listView.getAdapter();   
  5. if (listAdapter == null) {  
  6. // pre-condition  
  7. return;  
  8. }  
  9.   
  10. int totalHeight = 0;  
  11. for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回数据项的数目  
  12. View listItem = listAdapter.getView(i, null, listView);  
  13. listItem.measure(0, 0); //计算子项View 的宽高  
  14. totalHeight += listItem.getMeasuredHeight(); //统计所有子项的总高度  
  15. }  
  16.   
  17. ViewGroup.LayoutParams params = listView.getLayoutParams();  
  18. params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
  19. //listView.getDividerHeight()获取子项间分隔符占用的高度  
  20. //params.height最后得到整个ListView完整显示需要的高度  
  21. listView.setLayoutParams(params);  
  22. }  
  23. }  

 2.网上有帖子说在ScrollView中添加一属性 android:fillViewport="true" ,这样就可以让ListView全屏显示了。在我机器上测试失败了。

3.重写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. }  
原文地址:https://www.cnblogs.com/awkflf11/p/5061981.html