0

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.

Pratham Vaidya
  • 85
  • 4
  • 13
  • @NJ you can give explanation link or something for read about `recyclerview`. Please don't be rude with new members. sir you can follow this link for recyclerview https://developer.android.com/guide/topics/ui/layout/recyclerview – Ashish Jun 21 '19 at 16:07
  • @Ashish there is Answer in my question. There is nothing to be rude. It's you who thinks like that. – N J Jun 21 '19 at 16:09
  • Ok I will try Recycler View – Pratham Vaidya Jun 21 '19 at 16:44
  • @Ashish I Implemented Recycler View but the problem is again the same the child content is not wrapping inside the Recycler View. See My updated codes – Pratham Vaidya Jun 21 '19 at 18:30

2 Answers2

0

You can try this library: FitGridView

Otherwise you can use Gridlayout, or a combinaision of LinearLayout but you need lots of calculations, depending on the min and max size of the card you want, the number of cards by line and row.

Matthew
  • 1,905
  • 3
  • 19
  • 26
Turvy
  • 882
  • 1
  • 8
  • 23
  • It will need a lots of calculations so I am trying to use ```RecyclerView``` instead but even that doesn't work. Please see the updated question if you can help? – Pratham Vaidya Jun 21 '19 at 18:38
0

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.

Pratham Vaidya
  • 85
  • 4
  • 13