简体   繁体   中英

GridView is not taking full space inside ExpandableListView in android

I have to create an ExpandableListView as shown in picture below

在此处输入图像描述

I have added GridView inside ExpandableListView but it shows only first line of GridView as you can see in the pic below在此处输入图像描述 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM