ScrollView+listview嵌套使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_28864443/article/details/83214741

ScrollView 建议使用【android.support.v4.widget.NestedScrollView】进行使用,不需要自定义重写ScrollView ,NestedScrollView用起来比自定义好用的多

主要解决listview 的高度问题即可
高度解决方案如下:

使用方法:
            if (lvAdapterLeft != null) {
                lvAdapterLeft.notifyDataSetChanged();
                DisplayUtil.setListViewHeightBasedOnChildren(lv_left);
            }


计算listview 高度方法
    /**
     * 根据列表的item计算出该ListView的实际高度
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listView.getCount() - 1));
        listView.setLayoutParams(params);
    }

    /**
     * 根据列表的item计算出该ListView的实际高度
     *
     * @param listView
     */
    public static int getListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return 0;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        return totalHeight;
    }

猜你喜欢

转载自blog.csdn.net/sinat_28864443/article/details/83214741
今日推荐