我的代码中有一个小问题...问题是我需要添加每个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行长度时,totalHeight
个ListView
项仍然会给我相同的结果。
有什么想改进我的方法吗?