Skip to content Skip to sidebar Skip to footer

Add Divideritemdecoration Effect In The Begining Of Recyclerview

I have a layout that consists of a TextView with a helper text on the top of the layout followed by a RecyclerView with the related items. I added a DividerItemDecoration to the R

Solution 1:

There is no way to do what you want using the build-in DividerItemDecoration class.

The divider is drawn in a two-step process. First, getItemOffsets() is used to add space to the bottom of each item in the RecyclerView. Then, onDraw() is used to draw the divider within that space.

A look at the source code:

@Override
publicvoidgetItemOffsets(Rect outRect, View view, RecyclerView parent,
        RecyclerView.State state) {
    ...
    if (mOrientation == VERTICAL) {
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
}

Here you can see that space is only added to the bottom (or right, in horizontal mode) of each item. There's no special case for the first item to give it a top offset as well.

privatevoiddrawVertical(Canvas canvas, RecyclerView parent) {
    ...
    finalintchildCount= parent.getChildCount();
    for (inti=0; i < childCount; i++) {
        finalViewchild= parent.getChildAt(i);
        parent.getDecoratedBoundsWithMargins(child, mBounds);
        finalintbottom= mBounds.bottom + Math.round(child.getTranslationY());
        finalinttop= bottom - mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    ...
}

Here you can see that the vertical boundaries for the mDivider drawable are computed based on bottom edge of each child view.

You could, of course, create your own implementation of RecyclerView.ItemDecoration, and create a special case for the first item in the list.

Edit: Here's a stripped-down and simplified ItemDecoration that is based on DividerItemDecoration but also draws a divider on top of the first item: https://gist.github.com/zizibaloob/0c6be3e1318257950507e9c614c8aa70

Solution 2:

You can use View in your xml to divide recyclerView and TextView as below :

<Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@color/colorPrimary" />

Post a Comment for "Add Divideritemdecoration Effect In The Begining Of Recyclerview"