【问题标题】:Calculating the Listview's Each rows height in android (listview in side the scrollview)在android中计算Listview的每行高度(滚动视图中的listview)
【发布时间】:2016-02-20 11:01:00
【问题描述】:

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

Android ListView rows in ScrollView not fully displayed - clipped

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

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

我不知道应该如何计算列表的高度我尝试了不同的方法,但由于列表中每一行的大小可变,它不起作用。如果有人知道如何计算它在适配器文件或主要活动/片段中的运行时间,请告诉我应该怎么做。

请帮助我并提前感谢。

【问题讨论】:

  • Listiview 本身有滚动视图
  • 将滚动视图放在另一个滚动视图中并不是一个好主意。
  • 通过上面的链接。它对您的使用充分,因为我认为您的问题解决方案就在那里
  • 解决您的问题可能更简单。为什么将ListView 放在ScrollView 中?你的结局是什么?

标签: android expandablelistview


【解决方案1】:

简单的解决方案。添加这一行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);
        }

【讨论】:

  • 不工作我现在试过了,我从链接数中获得相同类型的代码一个或多个行被添加/删除到这个方法,否则这个方法中的整个代码几乎是相同的搜索。
  • 请帮助我从 3-4 天开始处理这个问题
  • 我在 xml 中也做了同样的事情,但没有什么对我有用。我不知道我在哪里犯了错误。顺便谢谢大家帮助我
  • 你好 Tamil Arasan 请接受来自 Deeplove9493@gmail.com 的邀请
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多