RecyclerView.ItemDecoration


public abstract class RecyclerView.ItemDecoration

Known direct subclasses
DividerItemDecoration

DividerItemDecoration is a RecyclerView.ItemDecoration that can be used as a divider between items of a LinearLayoutManager.

ItemTouchHelper

This is a utility class to add swipe to dismiss and drag &drop support to RecyclerView.


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 onDrawOver.

Summary

Public constructors

Public methods

void
getItemOffsets(
    @NonNull Rect outRect,
    int itemPosition,
    @NonNull RecyclerView parent
)

This method is deprecated.

Use getItemOffsets

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

Retrieve any offsets for the given item.

void

This method is deprecated.

Override onDraw

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

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

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.

Public constructors

ItemDecoration

Added in 1.0.0
public ItemDecoration()

Public methods

getItemOffsets

Added in 1.0.0
Deprecated in 1.0.0
public void getItemOffsets(
    @NonNull Rect outRect,
    int itemPosition,
    @NonNull RecyclerView parent
)

getItemOffsets

Added in 1.0.0
public void getItemOffsets(
    @NonNull Rect outRect,
    @NonNull View view,
    @NonNull RecyclerView parent,
    @NonNull 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
@NonNull Rect outRect

Rect to receive the output.

@NonNull View view

The child view to decorate

@NonNull RecyclerView parent

RecyclerView this ItemDecoration is decorating

@NonNull RecyclerView.State state

The current state of RecyclerView.

onDraw

Added in 1.0.0
Deprecated in 1.0.0
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent)

onDraw

Added in 1.0.0
public void onDraw(
    @NonNull Canvas c,
    @NonNull RecyclerView parent,
    @NonNull 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
@NonNull Canvas c

Canvas to draw into

@NonNull RecyclerView parent

RecyclerView this ItemDecoration is drawing into

@NonNull RecyclerView.State state

The current state of RecyclerView

onDrawOver

Added in 1.0.0
Deprecated in 1.0.0
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent)

onDrawOver

Added in 1.0.0
public void onDrawOver(
    @NonNull Canvas c,
    @NonNull RecyclerView parent,
    @NonNull RecyclerView.State state
)

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
@NonNull Canvas c

Canvas to draw into

@NonNull RecyclerView parent

RecyclerView this ItemDecoration is drawing into

@NonNull RecyclerView.State state

The current state of RecyclerView.