Home > Software engineering >  Render specific item count on a GridView
Render specific item count on a GridView

Time:07-23

I was working with the GridView for a while. Everything's working fine on runtime.

However, I have always exactly 5 items on my GridView, and I am looking to make it "more realistic" when rendering it on the "preview" layout.xml : now it "preview" a default number of items depending of its height (let's say for example 24 items).

Every single item can change its height
So, is there any possibility to achieve this by any xml tag, or by overriding the GridView ? Also without proceeding that by the Height would be very clean.

Here is my GridView on my xml layout

<GridView
            android:id="@ id/my_gridview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="3"/>

CodePudding user response:

I didn't find appropriate xml tag for your purpose and I think there isn't a built-in way for it. but I suggest using recyclerview gridlayoutmanager as @CommonsWare is pointing out. and then you could use tools:itemCount xml attribute to specify your list size.

of course this suggestions are valid only if preview is so important to you.

CodePudding user response:

Thanks to @mrzbn for the suggested answer. Works totally fine. Combining it with this answer preview of a RecyclerView's contents as Grid
Here is an example of what it worked for me, for whoever had to implement something similar :

<androidx.recyclerview.widget.RecyclerView
            android:id="@ id/my_grid_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            tools:itemCount="5"
            tools:orientation="vertical"
            tools:scrollbars="vertical"
            tools:spanCount="3"
            tools:listitem="@layout/my_sample_item"/>
  • Related