DividerItemDecoration Horizontal not appearing in center

1.1k Views Asked by At

I am using DividerItemDecoration from support library.

following is code to add decoration

 GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
 recyclerView.setLayoutManager(gridLayoutManager);
 recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL));

But the dividers are not appearing in center horizontally.

Following is screen shot of it.

enter image description here

Following is item layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingTop="8dp">


<ImageView
    android:id="@+id/imgFacility"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="center"
    android:foreground="@drawable/page_main_category_home_grid_selector"
    android:gravity="center"
    android:src="@drawable/plane_img"
    android:tint="?colorAccent" />


<TextView
    android:id="@+id/txtFacilityName"
    mediumFontPath="fonts/montserrat-medium.ttf"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="2dp"
    android:ellipsize="end"
    android:gravity="center"
    android:maxLines="2"
    android:minLines="2"
    android:text="Facility\nEWlall"
    android:textColor="#353535"
    android:textSize="12sp"
    tools:ignore="MissingPrefix" />

 </LinearLayout>

Also I tried using GridDividerDecoration

Following is output of it enter image description here

Given background color to each layout this is how it looks

enter image description here

1

There are 1 best solutions below

1
On

First create class using below code

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final int[] ATTRS = {android.R.attr.listDivider};

private Drawable mDivider;
private int mInsets;

public DividerItemDecoration(Context context) {
    TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();

    mInsets = 1;
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    drawVertical(c, parent);
    drawHorizontal(c, parent);
}

/**
 * Draw dividers at each expected grid interval
 */
public void drawVertical(Canvas c, RecyclerView parent) {
    if (parent.getChildCount() == 0) return;

    final int childCount = parent.getChildCount();

    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params =
                (RecyclerView.LayoutParams) child.getLayoutParams();

        final int left = child.getLeft() - params.leftMargin - mInsets;
        final int right = child.getRight() + params.rightMargin + mInsets;
        final int top = child.getBottom() + params.bottomMargin + mInsets;
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

/**
 * Draw dividers to the right of each child view
 */
public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int childCount = parent.getChildCount();

    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params =
                (RecyclerView.LayoutParams) child.getLayoutParams();

        final int left = child.getRight() + params.rightMargin + mInsets;
        final int right = left + mDivider.getIntrinsicWidth();
        final int top = child.getTop() - params.topMargin - mInsets;
        final int bottom = child.getBottom() + params.bottomMargin + mInsets;
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    //We can supply forced insets for each item view here in the Rect
    outRect.set(mInsets, mInsets, mInsets, mInsets);
}

}

After this use this decoration class in your recyclarview like this

recyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));

It help to show vertical and horizontal line between item.