I have a RecyclerView implemented using the GridLayoutManager.
Depending on how many items are in the data set, the spanCount is between 1-4. The item widths change depending on the spanCount. If the spancount is 4 or above, the spanCount is left at 4.
If I have 5 items, this leaves 1 item left over (4 items in 1 row, 1 item left over), positioned at the left of the screen, on a row by itself. I would like it to be centered.
I have tried setting the left over item's margin's programatically, and wrapping both the recyclerView and individual items in LinearLayouts and setting the gravity to center, as well as setting the gravity of the recyclerView.
Example item XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:id="@+id/relative"
/>
</LinearLayout>
Example RecyclerView XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Can anyone suggest a solution or, give me material to work from.
Thanks in advance.