ScrollerView中嵌套多个ListView,ListView都展示出

来源:互联网 发布:mac地址能不能修改 编辑:程序博客网 时间:2024/04/25 19:54

方法一:通过重写listView(此方法可用于使ListView高度自适应,GridView同样适用)

public class SearchListView extends ListView {    public SearchListView(Context context) {        // TODO Auto-generated constructor stub        super(context);    }    public SearchListView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    public SearchListView(Context context, AttributeSet attrs, int a) {        super(context, attrs, a);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expendSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expendSpec);    }}

方法 二:(此方法在设置的时候注意listView中设置的padding,如果有应在计算height时添加上)

private void updateHeight(ListView listview) {        ListAdapter listAdapter = listview.getAdapter();        if (listAdapter == null) {            return;        }        int totalHeight = 0;        for (int i = 0; i < listAdapter.getCount(); i++) {            View viewItem = listAdapter.getView(i, null, listview);            totalHeight += viewItem.getMeasuredHeight();        }        ViewGroup.LayoutParams param = listview.getLayoutParams();        param.height = totalHeight                + (listview.getDividerHeight() * (listAdapter.getCount() - 1));        listview.setLayoutParams(param);    }
0 0
原创粉丝点击