Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView Grid - define number of items per row in

I'm typically against posting a question without code, but I have no code to show. I'm finally converting my project from listviews and gridviews to recyclerView. In one class, I use a small gridview to place items pulled from a server.

The max number of items per row is 3, but the remainder needs to be on top. So if there's 5 items, top row has 2, bottom has 3. If there's 4 items, top row has 1, bottom has 3.

My google-fu is failing me and I'm not able to find any examples with this type of customization. If somebody could point me in the right direction, I'll post my completed code for those who find this post in the future.

Thanks all

like image 560
Psest328 Avatar asked Jul 01 '15 20:07

Psest328


2 Answers

There is a constructor of GridLayoutManager, that takes four parameters.

new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, true)

the second one is the spanCount, the number of columns or rows in the grid, while the forth is called reverseLayout and when set to true, the cells are laid out from the end to the start .

like image 121
Blackbelt Avatar answered Sep 21 '22 09:09

Blackbelt


Latest update

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/daysGuideline"
            app:spanCount="3" />

You can use spanCount as above

like image 38
Abraham Mathew Avatar answered Sep 25 '22 09:09

Abraham Mathew