I want to display list in recyclerView with grid layout with 2 columns. But when i apply item decor class then item is cutting from right side. Please checl below screenshot and code for the same. Do let me know if anyone have any idea on this.
Row file xml :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.sg.gbb.ui.tourplantcollection.viewmodel.PlantCollectionAdapterViewModel" />
</data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="164dp"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/screen_bg"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="164dp"
android:contentDescription="@string/gbb_app_name"
android:scaleType="fitXY"
android:src="@color/flower_bg" />
<com.sg.gbb.ui.custom.GBBTextView
android:id="@+id/tvOrderOn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="@{viewModel.code}"
android:textColor="@color/greyishBrown"
android:textSize="15sp"
app:customFont="@string/gardens_light" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
My recyclerView file :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.sg.gbb.ui.tourplantcollection.viewmodel.PlantCollectionViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/black_80_opacity"
android:contentDescription="@string/gbb_app_name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="@drawable/bg_edit_profile"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<com.sg.gbb.ui.custom.GBBTextView
android:id="@+id/titleTextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="@string/plant_collection_text"
android:textColor="@color/greyishBrown"
android:textSize="15sp"
app:customFont="@string/gardens_bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/plantCollectionRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:gravity="center"
android:scrollbars="none"
app:adapter="@{viewModel.plantList}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
tools:listitem="@layout/item_plant_collection" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/divider" />
<com.sg.gbb.ui.custom.GBBButton
android:id="@+id/cancelButton"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/bg_dismiss_button"
android:onClick="@{() -> viewModel.onBackClicked()}"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:paddingEnd="16dp"
android:paddingBottom="12dp"
android:text="@string/back"
android:textAllCaps="true"
android:textColor="@color/done_text"
android:textSize="@dimen/_12ssp"
app:buttonFont="@string/gardens_bold" />
</LinearLayout>
</RelativeLayout>
</layout>
Item decor file :
import android.graphics.Rect;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;
/**
* Created by Android Studio
* User: Ailurus(ailurus@foxmail.com)
* Date: 2015-10-28
* Time: 15:20
*/
public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
private int headerNum;
public EqualSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge, int headerNum) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
this.headerNum = headerNum;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view) - headerNum; // item position
if (position >= 0) {
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
} else {
outRect.left = 0;
outRect.right = 0;
outRect.top = 0;
outRect.bottom = 0;
}
}
}
