計算listview一項高度

/**
 * 計算item高度
 */
public void countItemHight(){
    totalHeight=0;
    for (int i = 0, len = popListviewAdapter.getCount(); i < len; i++) {
        View listItem = popListviewAdapter.getView(i, null, pop_listview);
        listItem.measure(0, 0); // 計算子項View 的寬高
        int list_child_item_height = listItem.getMeasuredHeight()+pop_listview.getDividerHeight();
        totalHeight += list_child_item_height; // 統計所有子項的總高度

        // listView.getDividerHeight()獲取子項間分隔符佔用的高度
        // params.height最後得到整個ListView完整顯示需要的高度
        ViewGroup.LayoutParams params = pop_listview.getLayoutParams();
        params.height = totalHeight + (pop_listview.getDividerHeight() *
                (popListviewAdapter.getCount() - 1));
        if(params.height>list_child_item_height*5){
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                    list_child_item_height*5);
            pop_listview.setLayoutParams(param);
        }else{
            pop_listview.setLayoutParams(params);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章