0

I have a RecyclerView with horizontal GridLayoutManager. In my app it is possible to change column height. If I increase the height of column everything works as expected: before: before after: after but if I decrease the height of column an empty space show up: space

cell xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/one_day_linearLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:minHeight="200dp"
>

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/one_day_recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="2dp"
    android:background="@color/colorAccent"
   />

I'm changing height directly on the ViewHolders. What should I do for remove this space between columns?

Marlen
  • 85
  • 2
  • 8

1 Answers1

0

RecyclerViews support the concept of ItemDecoration: special offsets and drawing around each element. As seen in this answer, you can use

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
  private int space;

  public SpacesItemDecoration(int space) {
    this.space = space;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, 
      RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;

    // Add top margin only for the first item to avoid double space between items
    if (parent.getChildLayoutPosition(view) == 0) {
        outRect.top = space;
    } else {
        outRect.top = 0;
    }
  }
}
Naitik
  • 990
  • 11
  • 31