Android 12 Developer Preview is here! Try it out, and give us your feedback!

ItemDecoration

abstract class ItemDecoration
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.ItemDecoration

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.

All ItemDecorations are drawn in the order they were added, before the item views (in onDraw() and after the items (in ItemDecoration#onDrawOver(Canvas, RecyclerView, * RecyclerView.State).

Summary

Public constructors

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set.

Public methods
open Unit
getItemOffsets(@NonNull outRect: Rect, itemPosition: Int, @NonNull parent: RecyclerView)

open Unit
getItemOffsets(@NonNull outRect: Rect, @NonNull view: View, @NonNull parent: RecyclerView, @NonNull state: RecyclerView.State)

Retrieve any offsets for the given item.

open Unit
onDraw(@NonNull c: Canvas, @NonNull parent: RecyclerView, @NonNull state: RecyclerView.State)

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

open Unit
onDraw(@NonNull c: Canvas, @NonNull parent: RecyclerView)

open Unit
onDrawOver(@NonNull c: Canvas, @NonNull parent: RecyclerView, @NonNull state: RecyclerView.State)

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

open Unit
onDrawOver(@NonNull c: Canvas, @NonNull parent: RecyclerView)

Public constructors

<init>

ItemDecoration()

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.

All ItemDecorations are drawn in the order they were added, before the item views (in onDraw() and after the items (in ItemDecoration#onDrawOver(Canvas, RecyclerView, * RecyclerView.State).

Public methods

getItemOffsets

open fun getItemOffsets(
    @NonNull outRect: Rect,
    itemPosition: Int,
    @NonNull parent: RecyclerView
): Unit

Deprecated: Use getItemOffsets(Rect, View, RecyclerView, State)

getItemOffsets

open fun getItemOffsets(
    @NonNull outRect: Rect,
    @NonNull view: View,
    @NonNull parent: RecyclerView,
    @NonNull state: RecyclerView.State
): Unit

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 RecyclerView#getChildAdapterPosition(View) to get the adapter position of the View.

Parameters
outRect Rect: Rect to receive the output.
view View: The child view to decorate
parent RecyclerView: RecyclerView this ItemDecoration is decorating
state RecyclerView.State: The current state of RecyclerView.

onDraw