I am trying to have a full screen grid with a RecyclerView
with a GridLayoutManager
but for some reason a horizontal space is added between the rows and at the top and bottom of my RecyclerView
. I can't get rid of it. I searched for solutions and tried an ItemDecorator
by setting the dimensions to 0 explicitly but that didn't work. Nothing else worked.
The image.
The code for my ItemDecorator
is pretty standard, included.
private class RecyclerViewItemDecorator extends RecyclerView.ItemDecoration {
private int spaceInPixels;
public RecyclerViewItemDecorator(int spaceInPixels) {
this.spaceInPixels = spaceInPixels;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = spaceInPixels;
outRect.right = spaceInPixels;
outRect.bottom = spaceInPixels;
if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = spaceInPixels;
} else {
outRect.top = 0;
}
}
}
These are the layout files I use.
/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.korah.moviesapp.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_fragment"
android:name="com.example.korah.moviesapp.MainActivityFragment"
tools:layout="@layout/activity_main_fragment"
>
</fragment>
</RelativeLayout>
/layout/activity_main_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/movies_recycler"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v7.widget.RecyclerView>
/layout/activity_movies_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/imageView"
android:src="@drawable/interst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
Should be because of the scaling of Image rather than anything else. So try setting scaleType of ImageView as fitXY.
<ImageView
android:id="@+id/imageView"
android:src="@drawable/interst"
android:layout_width="wrap_content"
android:scaleType="fitXY"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With