GridView is not taking full space inside ExpandableListView in android

59 Views Asked by At

I have to create an ExpandableListView as shown in picture below

enter image description here

I have added GridView inside ExpandableListView but it shows only first line of GridView as you can see in the pic below enter image description here GridView is only showing 4 items even though it has total 9 items but that user can see if he scrolls.

Here is the list_item layout in which i defined GridView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <GridView
        android:id="@+id/gvCategories"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="4"
        android:layout_marginStart="@dimen/margin_xxlarge"
        android:layout_marginEnd="@dimen/margin_xxlarge"
        android:fillViewport="true"/>

</LinearLayout>

How can I implement it in such a way that GridView doesnt go for scroll and always show all the entries inside it?

1

There are 1 best solutions below

0
Zohab Ali On

I had to create custom class for Gridview like this

import android.content.Context
import android.util.AttributeSet
import android.widget.GridView


    class ExpandableHeightGridView : GridView {
        var isExpanded = false
    
        constructor(context: Context?) : super(context){}
        constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
        constructor(
            context: Context?, attrs: AttributeSet?,
            defStyle: Int
        ) : super(context, attrs, defStyle) {
        }
    
        public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
            if (isExpanded) {
                val expandSpec = MeasureSpec.makeMeasureSpec(
                    MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST
                )
                super.onMeasure(widthMeasureSpec, expandSpec)
                val params = layoutParams
                params.height = measuredHeight
            } else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            }
        }
    }

then used it in xml like this

<com.my.path.to.customView.ExpandableHeightGridView
        android:id="@+id/gvCategories"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="4"
        android:layout_marginStart="@dimen/margin_xxlarge"
        android:layout_marginEnd="@dimen/margin_xxlarge"
        android:fastScrollEnabled="true"
        android:fillViewport="true"/>

and then had to set isExpanded to true in ExpandableList Adapter (getChildView) like this

val expandedListGridView = convertView!!.findViewById<ExpandableHeightGridView>(R.id.gvCategories)
expandedListGridView.isExpanded = true