Search code examples
androidgridviewkotlingridadapter

How to make GridView wrap all its child views in Kotlin


I want to group my card layout dynamically in a grid form but I am not sure about using GridView or GridLayout to do so. As the child views are dynamically added thus I don't know the number of rows or columns so I can't go for GridLayout but if I implement the GridView then content is scrolling which i don't want it to.

[ I want the grid view two wrap all the child without showing any scroll ]

So, either I should disable scroll of GridView to wrap its content or I am lacking some where else.

I tried to wrap the child views using a custom view. But even that doesn't solve the problem.

This is the custom view class:

package ...

import ...

class WrappingGridView : GridView {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var heightSpec = heightMeasureSpec
        if (layoutParams.height == LayoutParams.WRAP_CONTENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
        }
        super.onMeasure(widthMeasureSpec, heightSpec)
    }

}

So I want to know how can I wrap the child views inside the gridView? Also, Should I use GridView , GridLayout or something else?

UPDATED CODES AFTER TRYING RECYCLER VIEW:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              tools:context=".ShopFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:background="@android:color/white">

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:layout_marginTop="5dp">

        <TextView android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:textSize="20sp"
                  android:padding="2dp"
                  android:layout_marginLeft="5dp"
                  android:layout_marginStart="5dp"
                  style="@style/TextAppearance.AppCompat.Title"
                  android:text="Trending Tastes"
                  android:textColor="@color/colorPrimaryDark"
                  android:drawableLeft="@drawable/ic_whatshot_gradient_900_24dp"
                  android:drawableStart="@drawable/ic_whatshot_gradient_900_24dp"
                  android:drawablePadding="5dp"/>

        <HorizontalScrollView android:layout_width="match_parent"
                              android:scrollbars="none"
                              android:id="@+id/circularCardCollection"
                              android:layout_height="wrap_content">

            <LinearLayout android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:orientation="horizontal">
                <include layout="@layout/card_small_layout"/>
                <include layout="@layout/card_small_layout"/>
                <include layout="@layout/card_small_layout"/>
                <include layout="@layout/card_small_layout"/>
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:textSize="20sp"
                  android:padding="2dp"
                  android:layout_marginLeft="5dp"
                  android:layout_marginStart="5dp"
                  style="@style/TextAppearance.AppCompat.Title"
                  android:text=" @Treat -Must Try"
                  android:textColor="@color/colorPrimaryDark"
                  android:drawableLeft="@drawable/ic_restaurant_gradient_24dp"
                  android:drawableStart="@drawable/ic_restaurant_gradient_24dp"
                  android:drawablePadding="5dp"/>
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:padding="10dp"
                                            android:horizontalSpacing="5dp"
                                            android:verticalSpacing="10dp"
                                            android:gravity="center"
                                            android:id="@+id/grid"/>
</RelativeLayout>


    </LinearLayout>
</LinearLayout>

In Main Fragment:

   gridview.layoutManager = GridLayoutManager(view.context,2,LinearLayoutManager.VERTICAL,false)

This doesn't fix the Problem. I looked on the solutions of Stack Question But still no result.


Solution

  • I solved the problem. The gridview was not able to wrap its child because the layout has no ScrollView.

    I came from Web Designing background and the basic property of a webage is to scroll according to its content but in android we have to add ScrollView to make an Activity Scroll.

    Just added a ScrollView as the top most element and it worked like a charm!

    <ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  tools:context=".ShopFragment"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
            //Other Views
    
    <GridView  android:layout_width="match_parent"
                  android:layout_height="wrap_content"/>
    </ScrollView>
    

    Also RecyclerView is an updated version of GridView so its better to use that.