解决ScrollView下嵌套ListView、GridView显示不全的问题

来源:互联网 发布:服务器防火墙软件 编辑:程序博客网 时间:2024/04/28 04:20

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(00); //计算子项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. }  
 
0 0
原创粉丝点击