Android ScrollView中嵌套ListView或GidView,导致ListView或GidView无法滚动或显示不全的问题

前言:
       按照android的标准,ScrollView中是不能嵌套具有滑动特性的View的,但是如果真的要这样设计,就不得不采用这种怪异的组合方式。
问题:
       先说下这种方式如果不做特殊处理时会出现的冲突和问题: 
       1、在SrollView中嵌套ListView,ListView的显示会有问题,只显示一行或显示效果与预期不同,这是因为android禁止这样使用,放入ScrollView中的ListView
            的高度是无法计算的。
       2、嵌套中的子ListView和GridvIew是无法滑动的,因为子控件的滑动事件会被外面的ScrollView吃掉,如果想让子控件可以滑动,只能强行的截取滑动的相关事件了。
解决方案:
方案1:重写ListView与GridView,让其失去滑动特性【推荐使用】: 
    
     重写GridView:

  package com.example.test;

  import android.content.Context;

  import android.util.AttributeSet;

  import android.widget.GridView;

 

  public class NoScrollGridView extends GridView{

       public NoScrollGridView(Context context, AttributeSet attrs){          

      super(context, attrs);     

    }

      public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){          

        int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

      super.onMeasure(widthMeasureSpec, mExpandSpec); 

    }

  }

     -----------------------------------------------------------------------------

     重写ListView:

  package com.example.test;

  import android.content.Context;

  import android.util.AttributeSet;

  import android.widget.ListView;

  public class NoScrollListView extends ListView{

       public NoScrollListView(Context context, AttributeSet attrs){        

      super(context, attrs);

    }

       public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){          

      int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

      super.onMeasure(widthMeasureSpec, mExpandSpec);

    }

  }

方案2:人工计算子控件的尺寸

  public class Utility {

    public static void setListViewHeightBasedOnChildren(ListView listView) {

    //获取ListView对应的Adapter

    ListAdapter listAdapter = listView.getAdapter();

    if (listAdapter == null) {

      // pre-condition return;

    }

    int totalHeight = 0;

    for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回数据项的数目

      View listItem = listAdapter.getView(i, null, listView);

      listItem.measure(0, 0); //计算子项View 的宽高

      totalHeight += listItem.getMeasuredHeight(); //统计所有子项的总高度

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    //listView.getDividerHeight()获取子项间分隔符占用的高度

    //params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params);

    }

  }

  

原理:
  设置完ListView的Adapter后,根据ListView的子项目重新计算ListView的高度,然后把高度再作为LayoutParams设置给ListView,这样它的高度就正确了,通过人工算取
    控件的应有高度,再设置给ListView
注意:此方案中子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。
 
最后,建议大家还是少用这样的设计,毕竟这种方式是不标准与不规范的。
 
  
方案3:强行的截取滑动(也就是给listview加监听,如果响应onTouch时就中断scrollview的响应)
  
           listview.setOnTouchListener(new OnTouchListener() { 
             public boolean onTouch(View v, MotionEvent event) {
                      if (event.getAction() == MotionEvent.ACTION_MOVE) {
                            scrollView.requestDisallowInterceptTouchEvent(true);
                      }
                      return false;
             }
          });
      
posted on 2014-01-10 16:16  清凉一夏之博客  阅读(445)  评论(0编辑  收藏  举报