解决ScrollView 与 ListView 的滑动冲突

来源:互联网 发布:网络时时彩案件判刑 编辑:程序博客网 时间:2024/05/22 05:26
    public static void setListViewHeightBasedOnChildren(ListView listView) {        ListAdapter listAdapter = listView.getAdapter();        if (listAdapter == null) {            return;        }        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);        int totalHeight = 0;        View view = null;        for (int i = 0; i < listAdapter.getCount(); i++) {            view = listAdapter.getView(i, view, listView);            if (view instanceof ViewGroup) {                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));            }            view.measure(0, View.MeasureSpec.UNSPECIFIED);            totalHeight += view.getMeasuredHeight();        }        ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));        listView.setLayoutParams(params);        listView.requestLayout();        listView.setOverScrollMode(View.OVER_SCROLL_NEVER);    }

注意:此listview adapter 中不能应用RelativeLayout

0 0
原创粉丝点击