当TextView没有单行时,ListView的总高度错误

时间:2016-04-04 14:12:47

标签: android listview

我的代码中有一个小问题...问题是我需要添加每个ListView项的高度(因为我已经在ScrollView中得到了它而且我没有'我想要有双滚动的地方......)。

这是我的代码,允许我这样做:

public static void getListViewSize(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;
    }
    //set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
        totalHeight += listItem.getPaddingTop() + listItem.getPaddingBottom();
    }
    //setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    // print height of adapter on log
    Log.i("height of listItem:", String.valueOf(totalHeight));
}

我在这里调用此函数:

final ReceivedAttachementsListArrayAdapter adapter = new ReceivedAttachementsListArrayAdapter(getActivity(), attachements);
mReceivedAttachementsListView.setAdapter(adapter);
ViewHelper.getListViewSize(mReceivedAttachementsListView);

当我的TextView为1行长度或3行长度时,totalHeightListView项仍然会给我相同的结果。

有什么想改进我的方法吗?

0 个答案:

没有答案