How to Make Android ListView or GridView Expandable inside ScrollView

We all know that placing a ListView or GridView inside ScrollView will make them becoming difficult to scroll and can not be expanded. Android doesn’t provide a built in API to handle this problem so we have to do a little hack by subclassing those two views.

This is hack i found on stackoverflow, from Neil Traft ’s answer:

ExpandableHeightListView

package net.londatiga.android.widget;

import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;

public class ExpandableHeightListView extends ListView
{

boolean expanded = false;

public ExpandableHeightListView(Context context)
    {
        super(context);
    }

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

    public ExpandableHeightListView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

ExpandableHeightGridView

package net.londatiga.android.widget;

import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
import android.content.Context;

public class ExpandableHeightGridView extends GridView
{

 boolean expanded = false;

 public ExpandableHeightGridView(Context context)
 {
 super(context);
 }

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

 public ExpandableHeightGridView(Context context, AttributeSet attrs,
 int defStyle)
 {
 super(context, attrs, defStyle);
 }

 public boolean isExpanded()
 {
 return expanded;
 }

 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
 {
 // HACK! TAKE THAT ANDROID!
 if (isExpanded())
 {
 // Calculate entire height by providing a very large height hint.
 // But do not use the highest 2 bits of this integer; those are
 // reserved for the MeasureSpec mode.
 int expandSpec = MeasureSpec.makeMeasureSpec(
 Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
 super.onMeasure(widthMeasureSpec, expandSpec);

 ViewGroup.LayoutParams params = getLayoutParams();
 params.height = getMeasuredHeight();
 }
 else
 {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }
 }

 public void setExpanded(boolean expanded)
 {
 this.expanded = expanded;
 }
}

How to use:

//ListView
ExpandableHeightListView listView = new ExpandableHeightListView(this);

listView.setAdapter(adapter);
listView.setExpanded(true);

//GridView
ExpandableHeightGridView gridView = new ExpandableHeightGridView(this);

gridView.setNumColumns(4);
gridView.setAdapter(adapter);
gridView.setExpanded(true);

As you can see, to make the ListView or GridView expandable, just call the setExapanded(true).

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Comments

  1. kleon says

    Hello,
    very nice video!
    i want to make exact same as shown in your video.
    Can you provide some resources that can help me do it.
    I use web service to fetch images and want to display them like google play , one big in 1st row, 2 smaller in 2nd row and so..
    I bit confused with the appropriate layout files

    Thanks in advance

  2. Yash Tripathi says

    hello i am new in android development and i placed this code

    ExpandableHeightListView listView = new ExpandableHeightListView(this);
     
    listView.setAdapter(adapter);
    listView.setExpanded(true);

    inside oncreate method but its not work for me please help me and provide full code of this example

  3. dharmesh says

    I m facing the problem in this when i implement onitemclick listner. i mean its not working which u given example

    can u plz tell me how to do that?

    Thnak You Very Much.

  4. Giorgio Zamparelli says

    Hi, thanks for the post.
    When I add many items to the ExpandableHeightGridView the scrollbar is automatically scrolled down.
    Anyway to avoid this?

Leave a Reply

Your email address will not be published. Required fields are marked *