如何将ListView放入滚动视图而不使其崩溃?我四处寻找解决这个问题的方法,而我唯一能找到的答案似乎是“不要将ListView放入滚动视图“.我还没有看到任何真正的解释为什么尽管如此。我能找到的唯一原因似乎是谷歌认为你不应该这么做。我知道所以我就这么做了。因此,问题是,如何才能将ListView放置到ScrollView中,而不将其折叠到其最小高度?
4 回答
Qyouu
TA贡献1786条经验 获得超11个赞
德玛西亚99
TA贡献1770条经验 获得超3个赞
LayoutParams.height
Utility.setListViewHeightBasedOnChildren(yourListView)
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}}
偶然的你
TA贡献1841条经验 获得超3个赞
ListViewScrollViewListViewScrollViewListViewListViewListViewListViewListView
添加回答
举报
0/150
提交
取消
