Reputation: 357
I'm trying to calculate the height of an expandable list view so i can show all the items incluing the headers so the user wont have to scroll to see the content.
This is the fragment where i show the listview. Next to it is a calendar:
<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>
I'm calculating the height in the setOnGroupExpandListener located on the fragment. First of all i get the height of two children for the headers:
height += ((expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight())*listAdapter.getGroupCount();
Then i calculate the height of the children using the next formula:
for (int i = 0; i < listAdapter.getGroupCount();i++){
if (expListView.isGroupExpanded(i)){
height += listAdapter.getChildrenCount(i)*(expListView.getChildAt(0).getHeight()+expListView.getDividerHeight())-expListView.getDividerHeight();
}
}
When i expand the first group a gap is placed between the listview and the calendar. When i expand the second group, a bigger gap is placed between them.
I've got the same formula to collapse them.
Should i use another formula to calculate the height?
Upvotes: 0
Views: 682
Reputation: 93569
You shouldn't be doing this at all. The point of a listview is to scroll. If you don't want it to scroll, just add it as views to a LinearLayout.
As for your algorithm- it won't work. A ListView doesn't know the heights of any of its items unless they're currently onscreen. So its actually not possible to calculate this.
Also, what you're doing wouldn't calculate the heights of all views- listviews only have children for items that are onscreen. They recycle items between those. So your algorithm would only get the height of the items currently onscreen.
Upvotes: 1