2016-09-23 77 views
0

我想要计算可展开列表视图的高度,以便我可以显示包含标题的所有项目,以便用户不必滚动以查看内容。计算高度的问题ExpandableListView

这是我显示listview的片段。在它旁边是一个日历:

<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();   
} 
} 

当我展开第一组间隙放置在列表视图和日历之间。当我展开第二组时,他们之间会有更大的差距。

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

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

All items collapsed

First item expanded

All items expanded

回答

1

你不应该在所有这样做。列表视图的要点是滚动。如果你不想滚动,只需将它作为视图添加到LinearLayout。

至于你的算法 - 它不会工作。 ListView不知道任何项目的高度,除非它们当前在屏幕上。所以它实际上不可能计算这个。

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

+0

问题是我需要显示一个项目列表,它必须展开/折叠,我找到的唯一解决方案是使用可展开的列表视图。如果你知道另一个解决方案,我将非常感谢 – n4h1n

+0

可扩展列表视图对此很有帮助 - 只要你想(或者至少不介意)滚动。如果你不想滚动,那么你不想使用任何形式的列表视图。在那个时候写下你自己的观点。虽然看你的例子 - 我认为滚动是完全合适的。 –