我试图找到一种方法来根据子项的高度来设置列表视图的高度。我遵循这里给出的解决方案:How can I put a ListView into a ScrollView without it collapsing?
public void setListViewHeightBasedOnChildren(ListView listView) {
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);
if(listItem != null){
listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}现在我在listview上调用上面的函数,如下所示:
public void displayReviews(ArrayList<Reviews> resultReviews){
// Hide the loading progress
hideReviewsLoading();
if(resultReviews != null && resultReviews.size() > 0){
mCurrentReviewList.onFetchFinished(resultReviews);
setListViewHeightBasedOnChildren(mCurrentReviewList.getListView());
}
else{
// Display a generic text to indicate no reviews are in yet
displayEmptyText();
}
}上面的mCurrentreviewList是一个ListFragment,它基本上有一个适配器来设置布局中的元素。
我遇到的问题是,它测量的每一种语言的高度都不准确。因此,最后,当所有列表项(评论)填充到包含它的整体列表视图时,永远不要完全显示所有列表项。它在下面的某个地方被切断--就像在10条评论中,只有7.5条。
我不知道我做错了什么。任何帮助和指导都将不胜感激!
发布于 2014-02-07 06:34:54
ListView应该滚动-所以理论上,您可以在其中包含无限多的项。不需要根据ListView中有多少项来设置它的长度。
如果确实需要设置长度,那么这就违背了使用ListView的目的。请使用其他视图,如垂直ScrollView或其他东西,以满足您的需要。
发布于 2015-12-02 12:18:53
我没有足够的声誉来发表评论,但是关于Falc0nit3的答案,我已经实现了,但最终遇到了内存不足的错误,其中有图像等。
这是因为for循环的第一行View listItem = listAdapter.getView(i, null, listView);。
传入null作为convertView参数将导致适配器膨胀列表中的每一行。这并不理想,所以应该传入一个已经膨胀的行,这样getView只需要替换该行中的内容(例如,自己膨胀列表的第一行并继续使用)。参见以下链接http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/中的“视图回收”
发布于 2016-08-24 13:55:50
falc0nit3的答案(接受的答案)对我有用,但是如果您使用列表除法器,我建议在for循环之后添加以下代码
totalHeight += listView.getDividerHeight() * i;因此,最后的代码片段如下所示:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
int i;
for (i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
//add divider height to total height as many items as there are in listview
totalHeight += listView.getDividerHeight()*i;
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}https://stackoverflow.com/questions/21620764
复制相似问题