MaterialDividerItemDecoration

public class MaterialDividerItemDecoration


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

    dividerItemDecoration = new MaterialDividerItemDecoration(recyclerView.getContext(),
            layoutManager.getOrientation());
    recyclerView.addItemDecoration(dividerItemDecoration);

For more information, see the component developer guidance and design guidelines.

Summary

Constants

static final int
static final int

Public fields

int

Public constructors

MaterialDividerItemDecoration(Context context, int orientation)
MaterialDividerItemDecoration(
    Context context,
    AttributeSet attrs,
    int orientation
)
MaterialDividerItemDecoration(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int orientation
)

Public methods

int

Returns the divider color.

int

Returns the divider's end inset.

int

Returns the divider's start inset.

int

Returns the thickness set on the divider.

void
getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)
int
boolean

Whether there's a divider after the last item of a RecyclerView.

void
onDraw(Canvas canvas, RecyclerView parent, State state)
void
setDividerColor(int color)

Sets the color of the divider.

void
setDividerColorResource(Context context, int colorId)

Sets the color of the divider.

void
setDividerInsetEnd(int insetEnd)

Sets the end inset of the divider.

void
setDividerInsetEndResource(Context context, int insetEndId)

Sets the end inset of the divider.

void
setDividerInsetStart(int insetStart)

Sets the start inset of the divider.

void
setDividerInsetStartResource(Context context, int insetStartId)

Sets the start inset of the divider.

void
setDividerThickness(int thickness)

Sets the thickness of the divider.

void
setDividerThicknessResource(Context context, int thicknessId)

Sets the thickness of the divider.

void
setLastItemDecorated(boolean lastItemDecorated)

Sets whether the class should draw a divider after the last item of a RecyclerView.

void
setOrientation(int orientation)

Sets the orientation for this divider.

Protected methods

boolean
shouldDrawDivider(int position, Adapter<?> adapter)

Whether a divider should be drawn below the current item that is being drawn.

Constants

HORIZONTAL

public static final int HORIZONTAL

VERTICAL

public static final int VERTICAL

Public fields

orientation

public int orientation

Public constructors

MaterialDividerItemDecoration

public MaterialDividerItemDecoration(Context context, int orientation)

MaterialDividerItemDecoration

public MaterialDividerItemDecoration(
    Context context,
    AttributeSet attrs,
    int orientation
)

MaterialDividerItemDecoration

public MaterialDividerItemDecoration(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int orientation
)

Public methods

getDividerColor

public int getDividerColor()

Returns the divider color.

ref com.google.android.material.R.styleable#MaterialDivider_dividerColor

See also
setDividerColor

getDividerInsetEnd

public int getDividerInsetEnd()

Returns the divider's end inset.

ref com.google.android.material.R.styleable#MaterialDivider_dividerInsetEnd

getDividerInsetStart

public int getDividerInsetStart()

Returns the divider's start inset.

ref com.google.android.material.R.styleable#MaterialDivider_dividerInsetStart

getDividerThickness

public int getDividerThickness()

Returns the thickness set on the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerThickness

getItemOffsets

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

getOrientation

public int getOrientation()

isLastItemDecorated

public boolean isLastItemDecorated()

Whether there's a divider after the last item of a RecyclerView.

ref com.google.android.material.R.styleable#MaterialDivider_shouldDecorateLastItem

onDraw

public void onDraw(Canvas canvas, RecyclerView parent, State state)

setDividerColor

public void setDividerColor(int color)

Sets the color of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerColor

Parameters
int color

The color to be set.

See also
getDividerColor

setDividerColorResource

public void setDividerColorResource(Context context, int colorId)

Sets the color of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerColor

Parameters
int colorId

The id of the color resource to be set.

See also
getDividerColor

setDividerInsetEnd

public void setDividerInsetEnd(int insetEnd)

Sets the end inset of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerInsetEnd

Parameters
int insetEnd

The end inset to be set.

setDividerInsetEndResource

public void setDividerInsetEndResource(Context context, int insetEndId)

Sets the end inset of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerInsetEnd

Parameters
int insetEndId

The id of the inset dimension resource to be set.

setDividerInsetStart

public void setDividerInsetStart(int insetStart)

Sets the start inset of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerInsetStart

Parameters
int insetStart

The start inset to be set.

setDividerInsetStartResource

public void setDividerInsetStartResource(Context context, int insetStartId)

Sets the start inset of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerInsetStart

Parameters
int insetStartId

The id of the inset dimension resource to be set.

setDividerThickness

public void setDividerThickness(int thickness)

Sets the thickness of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerThickness

Parameters
int thickness

The thickness value to be set.

setDividerThicknessResource

public void setDividerThicknessResource(Context context, int thicknessId)

Sets the thickness of the divider.

ref com.google.android.material.R.styleable#MaterialDivider_dividerThickness

Parameters
int thicknessId

The id of the thickness dimension resource to be set.

setLastItemDecorated

public void setLastItemDecorated(boolean lastItemDecorated)

Sets whether the class should draw a divider after the last item of a RecyclerView.

ref com.google.android.material.R.styleable#MaterialDivider_lastItemDecorated

Parameters
boolean lastItemDecorated

whether there's a divider after the last item of a recycler view.

setOrientation

public void setOrientation(int orientation)

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

A HORIZONTAL orientation will draw a vertical divider, and a VERTICAL orientation a horizontal divider.

Parameters
int orientation

The orientation of the RecyclerView this divider is associated with: HORIZONTAL or VERTICAL

Protected methods

shouldDrawDivider

protected boolean shouldDrawDivider(int position, Adapter<?> adapter)

Whether a divider should be drawn below the current item that is being drawn.

Note: if lasItemDecorated is false, the divider below the last item will never be drawn even if this method returns true.

Parameters
int position

the position of the current item being drawn.

Adapter<?> adapter

the RecyclerView.Adapter associated with the item being drawn.