1. 程式人生 > >ScrollView+listview巢狀使用

ScrollView+listview巢狀使用

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;
    }