DividerItemDecoration

public class DividerItemDecoration extends RecyclerView.ItemDecoration


DividerItemDecoration is a RecyclerView.ItemDecoration that can be used as a divider between items of a LinearLayoutManager. It supports both HORIZONTAL and VERTICAL orientations.

    mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
            mLayoutManager.getOrientation());
    recyclerView.addItemDecoration(mDividerItemDecoration);

Summary

Constants

static final int
static final int

Public constructors

DividerItemDecoration(Context context, int orientation)

Creates a divider RecyclerView.ItemDecoration that can be used with a LinearLayoutManager.

Public methods

@Nullable Drawable
void
getItemOffsets(
    Rect outRect,
    View view,
    RecyclerView parent,
    RecyclerView.State state
)

Retrieve any offsets for the given item.

void

Draw any appropriate decorations into the Canvas supplied to the RecyclerView.

void

Sets the Drawable for this divider.

void
setOrientation(int orientation)

Sets the orientation for this divider.

Inherited methods

From androidx.recyclerview.widget.RecyclerView.ItemDecoration
void
getItemOffsets(
    @NonNull Rect outRect,
    int itemPosition,
    @NonNull RecyclerView parent
)

This method is deprecated.

Use getItemOffsets

void

This method is deprecated.

Override onDraw

void

This method is deprecated.

Override onDrawOver

void
onDrawOver(
    @NonNull Canvas c,
    @NonNull RecyclerView parent,
    @NonNull RecyclerView.State state
)

Draw any appropriate decorations into the Canvas supplied to the RecyclerView.

Constants

HORIZONTAL

Added in 1.0.0
public static final int HORIZONTAL = 0

VERTICAL

Added in 1.0.0
public static final int VERTICAL = 1

Public constructors

DividerItemDecoration

Added in 1.0.0
public DividerItemDecoration(Context context, int orientation)

Creates a divider RecyclerView.ItemDecoration that can be used with a LinearLayoutManager.

Parameters
Context context

Current context, it will be used to access resources.

int orientation

Divider orientation. Should be HORIZONTAL or VERTICAL.

Public methods

getDrawable

Added in 1.1.0
public @Nullable Drawable getDrawable()
Returns
@Nullable Drawable

the Drawable for this divider.

getItemOffsets

public void getItemOffsets(
    Rect outRect,
    View view,
    RecyclerView parent,
    RecyclerView.State state
)

Retrieve any offsets for the given item. Each field of outRect specifies the number of pixels that the item view should be inset by, similar to padding or margin. The default implementation sets the bounds of outRect to 0 and returns.

If this ItemDecoration does not affect the positioning of item views, it should set all four fields of outRect (left, top, right, bottom) to zero before returning.

If you need to access Adapter for additional data, you can call getChildAdapterPosition to get the adapter position of the View.

Parameters
Rect outRect

Rect to receive the output.

View view

The child view to decorate

RecyclerView parent

RecyclerView this ItemDecoration is decorating

RecyclerView.State state

The current state of RecyclerView.

onDraw

public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)

Draw any appropriate decorations into the Canvas supplied to the RecyclerView. Any content drawn by this method will be drawn before the item views are drawn, and will thus appear underneath the views.

Parameters
Canvas c

Canvas to draw into

RecyclerView parent

RecyclerView this ItemDecoration is drawing into

RecyclerView.State state

The current state of RecyclerView

setDrawable

Added in 1.0.0
public void setDrawable(@NonNull Drawable drawable)

Sets the Drawable for this divider.

Parameters
@NonNull Drawable drawable

Drawable that should be used as a divider.

setOrientation

Added in 1.0.0
public void setOrientation(int orientation)

Sets the orientation for this divider. This should be called if RecyclerView.LayoutManager changes orientation.

Parameters
int orientation

HORIZONTAL or VERTICAL