Im trying to achieve a recyclerView with a gridLayout with 3 columns and 2 rows.
i need the first column aligned to the start parent and the third row aligned to the end of the parent. Of course the second column has to be centered in the parent.
How can i achieve this?
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewAccessHome"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="28dp"
app:layout_constraintTop_toBottomOf="@id/textViewAccesos"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:listitem="@layout/cell_accesos_menu_home"
app:layoutManager="GridLayoutManager"
app:spanCount="3"
tools:itemCount="6"
android:clipToPadding="false"/>
And the cell
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_menu_access_home"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="100dp"
android:paddingHorizontal="15dp"
android:paddingVertical="17dp"
android:layout_marginBottom="15dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageViewCellRWHAccessHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic__close"/>
<TextView
android:id="@+id/textViewCellRWHAccessHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text here"
android:layout_marginTop="10dp"
android:fontFamily="@font/roboto_medium"
android:lines="2"
android:textSize="12sp"
android:textAlignment="center"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</com.google.android.material.card.MaterialCardView>
I achieved it with this code, idk if it is correct.
recyclerViewAccessHome.addItemDecoration(HomeItemDecorator())
class HomeItemDecorator : RecyclerView.ItemDecoration() {
private var space = 22
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: State) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
outRect.top = space;
if(parent.getChildAdapterPosition(view) == 2 || parent.getChildAdapterPosition(view) == 5) {
outRect.right = 0;
}
if(parent.getChildAdapterPosition(view) == 0 || parent.getChildAdapterPosition(view) == 3) {
outRect.left = 0;
}
if(parent.getChildAdapterPosition(view) == 1 || parent.getChildAdapterPosition(view) ==4) {
outRect.left = space / 2;
outRect.right = space / 2;
}
}
}