atearsan
8/1/2015 - 3:37 AM

scrollview 嵌套 listview 导致listview显示不完整解决方式

scrollview 嵌套 listview 导致listview显示不完整解决方式

/**
     * 重新计算listview高度<br/>
     * <i>
     * scrollview 嵌套 listview, 会导致listview只显示一个, 所以在listview.setAdapter之后重新计算高度<br/>
     * listview 必须是linearlayout布局
     * </i>
     *
     * @param listView listview
     */
    public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            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() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }