【问题标题】:Issues calculating Height ExpandableListView计算高度 ExpandableListView 的问题
【发布时间】:2017-02-01 19:30:35
【问题描述】:

我正在尝试计算可扩展列表视图的高度,以便我可以显示包括标题在内的所有项目,这样用户就不必滚动查看内容。

这是我显示列表视图的片段。旁边是日历:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".fragments.DashboardFragment">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context=".fragments.DashboardFragment">
        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <CalendarView
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="292dp" />
    </LinearLayout>
</LinearLayout>

我正在计算位于片段上的 setOnGroupExpandListener 中的高度。首先,我得到两个孩子的高度作为标题:

height += ((expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight())*listAdapter.getGroupCount();

然后我使用下一个公式计算孩子的身高:

for (int i = 0; i <  listAdapter.getGroupCount();i++){
if (expListView.isGroupExpanded(i)){        
    height += listAdapter.getChildrenCount(i)*(expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight();        
}
}

当我展开第一组时,列表视图和日历之间会出现一个间隙。当我扩大第二组时,它们之间的差距更大。

我有相同的公式来折叠它们。

我应该使用另一个公式来计算高度吗?

【问题讨论】:

    标签: android listview height expandablelistview


    【解决方案1】:

    你根本不应该这样做。列表视图的重点是滚动。如果您不希望它滚动,只需将其作为视图添加到 LinearLayout。

    至于你的算法——它行不通。 ListView 不知道其任何项目的高度,除非它们当前在屏幕上。所以这实际上是不可能计算出来的。

    此外,您所做的不会计算所有视图的高度 - 列表视图只有屏幕上的项目的子级。他们在这些之间回收物品。所以你的算法只会得到当前屏幕上项目的高度。

    【讨论】:

    • 问题是我需要显示一个项目列表,它必须展开/折叠,我发现的唯一解决方案是使用可展开的列表视图。如果您知道其他解决方案,我将不胜感激
    • 可扩展的列表视图非常适合 - 只要您想要(或至少不介意)滚动。如果您不想滚动,那么您不想使用任何形式的列表视图。在这一点上,是时候写下你自己的观点了。虽然看你的例子 - 我认为滚动是完全合适的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 2011-09-28
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多