在 android 中计算列表视图的每一行高度(滚动视图中的列表视图)

Calculating the Listview's Each rows height in android (listview in side the scrollview)

我正在开发一个应用程序,我必须在滚动视图中显示列表视图,问题是我没有获得正确的列表视图高度我已经遵循了下面链接中的几乎所有代码。

Android ListView rows in ScrollView not fully displayed - clipped

How can I put a ListView into a ScrollView without it collapsing?

但每次我得到这样的列表视图

我不知道应该如何计算列表的高度我没有尝试过任何不同的方法,但是由于列表中每一行的大小可变,它不起作用。如果有人知道如何计算它 运行 时间在适配器文件中或从主 activity/fragment 让我知道应该怎么做。

请帮助我并提前致谢。

简单的解决方案。添加此行 setListViewHeightBasedOnChildren(item_Listview); 和下面的方法

if(item_lists != null && item_lists.size() > 0) {
                itl = new Itemlist(getActivity(), item_lists);
                item_Listview.setAdapter(itl);
                setListViewHeightBasedOnChildren(item_Listview);
            }



    private void setListViewHeightBasedOnChildren(ListView item_listview) {
            ListAdapter listAdapter = item_listview.getAdapter();
            if (listAdapter == null) {
                return;
            }
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, item_listview);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = item_listview.getLayoutParams();
            params.height = totalHeight
                    + (item_listview.getDividerHeight() * (listAdapter.getCount() - 1));
            item_listview.setLayoutParams(params);
        }