scrollView 嵌套 listview 方式除了测量还有什么方法?

1、手动设置 ListView 高度
经过测试发现,在 xml 中直接指定 ListView 的高度,是可以解决这个问题的,但是 ListView
中的数据是可变的,实际高度还需要实际测量。
于是手动代码设置 ListView 高度的方法就诞生了。 /**
* 动态设置 ListView 的高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 动态设置 ListView 的高度
* @param listView
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    if(listView == null)
        return;
    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);
}                       

 

 

posted on   LoaderMan  阅读(178)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示