总结ScrollView嵌套ListView的解决方法

在开发过程中难免会遇到ScrollView嵌套ListView的情况,对于这种情况有几种比较好的解决方案。
一、当ListView的Item为固定高度时,可以通过继承ListView重写onMeasure方法。
二、通过手动计算高度设置LayoutParams可以完美解决;
三、但当ListView的Item的高度不固定时,使用上面两种方法会有一个bug,ListView的最后一个Item偶尔会出现显示不全的问题,通过文中的第三种方法可以完美解决此问题。

ListView中的Item为固定高度
一、可通过自定义ListView重写onMeasure方法。

public class MyListView extends ListView {  

    public MyListView(Context context) {  
        super(context);  
    }  

    public MyListView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  

    public MyListView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
    }  

    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
                MeasureSpec.AT_MOST);  
        super.onMeasure(widthMeasureSpec, expandSpec);  
    }  
}

二、通过手动计算ListView的高度解决此问题。
注意这个方法只需要在setAdapter后调用。

//调用示范
ListView listView = (ListView) findViewById(id);
YourAdapter adapter = new MyAdapter("初始化你的适配器");
listView.setAdapter(adapter);
setListViewHeightBasedOnChildren(listView);

//自定义计算ListView高度方法
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);
}

但是这个方法有个两个细节需要注意:
一是Adapter中getView方法返回的View的必须由LinearLayout组成,因为只有LinearLayout才有measure()方法,如果使用其他的布局如RelativeLayout,在调用listItem.measure(0, 0);时就会抛异常,因为除LinearLayout外的其他布局的这个方法就是直接抛异常的。
二是使用这个方法的话,默认在ScrollView顶端的项是ListView。主要是因为在界面在计算高度的时候最初并未计算ListView的高度,因此当我们自定义计算高度的时候便会出现这种情况。解决方法:

方法一:获取ScrollView第一个控件的焦点保持最前
     mTextView.setFocusable(true);//获取焦点保持最前
     mTextView.setFocusableInTouchMode(true);
     mTextView.requestFocus();
方法二:直接使用ScrollView的smoothScrollTo方法
        myScrollView.smoothScrollTo(0,20)
        lv.setFocusable(false);//去掉listview的焦点



ListView中的Item高度不固定时

三丶当ListView的Item的高度不固定时,使用上面两种方法会有一个bug,ListView的最后一个Item偶尔会出现显示不全的问题,使用下面的类可以完美解决此问题。

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                }
                newHeight += getDividerHeight() * listPosition;
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}

好了,总而言之各有个的优势。我们可以根据我们自己的需求去选择对应的方式,方法的使用在于灵活贯通,相信还有其他更好的方法我们可以去使用。因为我始终相信 技术 永无止境

参考资料链接:
http://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view
http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值