计算高度 ExpandableListView 的问题
Issues calculating Height ExpandableListView
我正在尝试计算可扩展列表视图的高度,这样我就可以显示包括 headers 在内的所有项目,这样用户就不必滚动查看内容了。
这是我显示列表视图的片段。旁边是日历:
<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 中的高度。首先,我得到 headers 的两个 children 的高度:
height += ((expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight())*listAdapter.getGroupCount();
然后我使用下一个公式计算 children 的高度:
for (int i = 0; i < listAdapter.getGroupCount();i++){
if (expListView.isGroupExpanded(i)){
height += listAdapter.getChildrenCount(i)*(expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight();
}
}
当我展开第一个组时,列表视图和日历之间出现了一个间隙。当我展开第二组时,它们之间的差距更大。
我有相同的公式来折叠它们。
我应该使用其他公式来计算身高吗?
你根本不应该这样做。列表视图的要点是滚动。如果您不想让它滚动,只需将它作为视图添加到 LinearLayout。
至于你的算法 - 它不会工作。 ListView 不知道其任何项目的高度,除非它们当前在屏幕上。所以实际上不可能计算这个。
此外,您所做的不会计算所有视图的高度 - 列表视图只有屏幕上项目的子项。他们在这些之间回收物品。所以你的算法只会得到当前屏幕上项目的高度。
我正在尝试计算可扩展列表视图的高度,这样我就可以显示包括 headers 在内的所有项目,这样用户就不必滚动查看内容了。
这是我显示列表视图的片段。旁边是日历:
<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 中的高度。首先,我得到 headers 的两个 children 的高度:
height += ((expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight())*listAdapter.getGroupCount();
然后我使用下一个公式计算 children 的高度:
for (int i = 0; i < listAdapter.getGroupCount();i++){
if (expListView.isGroupExpanded(i)){
height += listAdapter.getChildrenCount(i)*(expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight();
}
}
当我展开第一个组时,列表视图和日历之间出现了一个间隙。当我展开第二组时,它们之间的差距更大。
我有相同的公式来折叠它们。
我应该使用其他公式来计算身高吗?
你根本不应该这样做。列表视图的要点是滚动。如果您不想让它滚动,只需将它作为视图添加到 LinearLayout。
至于你的算法 - 它不会工作。 ListView 不知道其任何项目的高度,除非它们当前在屏幕上。所以实际上不可能计算这个。
此外,您所做的不会计算所有视图的高度 - 列表视图只有屏幕上项目的子项。他们在这些之间回收物品。所以你的算法只会得到当前屏幕上项目的高度。