避免在SrollView里面嵌套的ListView或者ExpandableListView无法全部展示数据

来源:互联网 发布:我的理想是不上班 知乎 编辑:程序博客网 时间:2024/05/06 01:31

代码如下所示

package com.lanbang.material.views;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;/** * 重写ListView,避免在SrollView里面的ListView无法全部展示数据,(避免只能展示一行,必须靠滚动条来滚动查看所有数据) *  *  */public class ListViewForScrollView extends ListView {    public ListViewForScrollView(Context context) {        super(context);    }    public ListViewForScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ListViewForScrollView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    /**     * 重写该方法,达到使ListView适应ScrollView的效果     *     */    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }}
如果是ExpandableListView嵌套在ScrollView里面,代码一样,只是将ListView修改为ExpandableListView,因为ExpandableListView是ListView的子类。

亲测可以实现。

0 0
原创粉丝点击