Set ListView height to display all available items
Asked Answered
R

3

6

I have a ListView inside a ScrollView.

How can I set the height of the ListView so that all the items in its adapter fit in it and it does not need to scroll.

Rhines answered 27/11, 2015 at 16:43 Comment(6)
You want to scroll the layout instead of the list?Mun
Don't put a listview in a scrollview (it not recommend). If you want a list of items that doesn't scroll, it's called a linearlayout.Anyhow
@UdiIdan yes, exactlyRhines
Nesting scrollable Views will result in a terrible user experience.Lollygag
@FrankN.Stein mdtuyen's NonScrollListView does not scroll. Is it still problematic?Rhines
Probably not. But it's a trick. It's forcing the CPU to do some work. While a LinearLayout does it natively.Lollygag
A
15

If you want a list of items that doesn't scroll, it's called a linearlayout.

Else if you want you can custom listview as:

public class NonScrollListView extends ListView {

    public NonScrollListView(Context context) {
        super(context);
    }
    public NonScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();    
    }
}
Anyhow answered 27/11, 2015 at 16:52 Comment(3)
perfect solution.Wildeyed
It works well. But how can I set height, padding,... for each item row?Wyeth
Inside a scrollview, it still tries to scroll the list and causes the outer scroll to stuckTheorist
M
1

You can use something like this, it will expand the list so it won't need scrolling. Just remember to wrap it in your XML with a ScrollView:

<ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

and the class:

public class ExpandedListView extends ListView {

private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;

public ExpandedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
    if (getCount() != old_count) {
        old_count = getCount();
        params = getLayoutParams();
        params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
        setLayoutParams(params);
    }

    super.onDraw(canvas);
}
}

You can also refer to this question which will be helpful: Calculate the size of a list view or how to tell it to fully expand

Mun answered 27/11, 2015 at 16:53 Comment(0)
M
-1

If you are using a ScrollView, it will scroll if the height of it's content would be bigger than the screen size. Any way, try to use the parameter bellow to see if it does what you want.

android:layout_height="wrap_content"

Malnourished answered 27/11, 2015 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.