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

Public methods
open Unit

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

open Unit

open Unit

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

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

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

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

Retrieve any offsets for the given item.

Public constructors

<init>

ItemDecoration()

Public methods

onDrawOver

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

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

Parameters
c Canvas: Canvas to draw into
parent Canvas: RecyclerView this ItemDecoration is drawing into
state Canvas: The current state of RecyclerView.

onDrawOver

open fun onDrawOver(c: Canvas, parent: RecyclerView): Unit

onDraw

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

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
c Canvas: Canvas to draw into
parent Canvas: RecyclerView this ItemDecoration is drawing into
state Canvas: The current state of RecyclerView

onDraw

open fun onDraw(c: Canvas, parent: RecyclerView): Unit

getItemOffsets

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

getItemOffsets

open fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, 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 Rect: The child view to decorate
parent Rect: RecyclerView this ItemDecoration is decorating
state Rect: The current state of RecyclerView.