Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure ListView to automatically change its height?

I have three ListView widgets in the same LinearLayout. Something like this (I'm omitting XML elements that are not relevant in this example):

<LinearLayout>
    <ListView
      android:layout_width="fill_parent"
      android:layout_height="360dp"/>
    <ListView
      android:layout_width="fill_parent"
      android:layout_height="360dp"/>
    <ListView
      android:layout_width="fill_parent"
      android:layout_height="360dp"/>
</LinearLayout>

This forces the list to have a height of 360 dip. Of course, that will be its height even if there are few list items. So, my question is how can make the lists have an automatic height? What I want is that the ListView height takes the exact size of the sum of all its list items.

like image 256
Cristian Avatar asked Dec 04 '22 11:12

Cristian


1 Answers

I've implemented it this way (code is work in progress so it's more a idea source than solution):

package com.customcontrols;
public class NoScrollListView extends ListView
{
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0) );

        // here I assume that height's being calculated for one-child only, seen it in ListView's source which is actually a bad idea
        int childHeight = getMeasuredHeight() - (getListPaddingTop() + getListPaddingBottom() +  getVerticalFadingEdgeLength() * 2);

        int fullHeight = getListPaddingTop() + getListPaddingBottom() + childHeight*(getCount());

        setMeasuredDimension(getMeasuredWidth(), fullHeight);
    }
}

the calculation's not perfect, but it's close and works so far. after that you just create a layout like this:

ScrollView com.customcontrol.NoScrollListView com.customcontrol.NoScrollListView com.customcontrol.NoScrollListView /ScrollView

The scrollView's crucial since you can easily run out of screen bounds.

PS. The calculation's rectum-driven since most of the calculation methods in ListView&Co are package private which is quite a strange choice for publicly inheritable classes for UI.

like image 184
durilka Avatar answered Feb 25 '23 06:02

durilka