MaterialDividerItemDecoration

public class MaterialDividerItemDecoration
extends RecyclerView.ItemDecoration

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.ItemDecoration
     ↳ com.google.android.material.divider.MaterialDividerItemDecoration


MaterialDividerItemDecoration is a RecyclerView.ItemDecoration, similar to a DividerItemDecoration, 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

XML attributes

MaterialDivider_dividerColor  
MaterialDivider_dividerInsetEnd  
MaterialDivider_dividerInsetStart  
MaterialDivider_dividerThickness  
MaterialDivider_lastItemDecorated  

Constants

int HORIZONTAL

int VERTICAL

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 getDividerColor()

Returns the divider color.

int getDividerInsetEnd()

Returns the divider's end inset.

int getDividerInsetStart()

Returns the divider's start inset.

int getDividerThickness()

Returns the thickness set on the divider.

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

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

void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.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.

Inherited methods

XML attributes

MaterialDivider_dividerColor

Related methods:

MaterialDivider_dividerInsetEnd

Related methods:

MaterialDivider_dividerInsetStart

Related methods:

MaterialDivider_dividerThickness

Related methods:

MaterialDivider_lastItemDecorated

Related methods:

Constants

HORIZONTAL

public static final int HORIZONTAL

Constant Value: 0 (0x00000000)

VERTICAL

public static final int VERTICAL

Constant Value: 1 (0x00000001)

Public constructors

MaterialDividerItemDecoration

public MaterialDividerItemDecoration (Context context, 
                int orientation)

Parameters
context Context

orientation int

MaterialDividerItemDecoration

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

Parameters
context Context

attrs AttributeSet

orientation int

MaterialDividerItemDecoration

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

orientation int

Public methods

getDividerColor

public int getDividerColor ()

Returns the divider color.

Related XML Attributes:

Returns
int

getDividerInsetEnd

public int getDividerInsetEnd ()

Returns the divider's end inset.

Related XML Attributes:

Returns
int

getDividerInsetStart

public int getDividerInsetStart ()

Returns the divider's start inset.

Related XML Attributes:

Returns
int

getDividerThickness

public int getDividerThickness ()

Returns the thickness set on the divider.

Related XML Attributes:

Returns
int

getItemOffsets

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

Parameters
outRect Rect

view View

parent RecyclerView

state RecyclerView.State

getOrientation

public int getOrientation ()

Returns
int

isLastItemDecorated

public boolean isLastItemDecorated ()

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

Returns
boolean

onDraw

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

Parameters
canvas Canvas

parent RecyclerView

state RecyclerView.State

setDividerColor

public void setDividerColor (int color)

Sets the color of the divider.

Related XML Attributes:

Parameters
color int: The color to be set.

See also:

setDividerColorResource

public void setDividerColorResource (Context context, 
                int colorId)

Sets the color of the divider.

Related XML Attributes:

Parameters
context Context

colorId int: The id of the color resource to be set.

See also:

setDividerInsetEnd

public void setDividerInsetEnd (int insetEnd)

Sets the end inset of the divider.

Related XML Attributes:

Parameters
insetEnd int: The end inset to be set.

setDividerInsetEndResource

public void setDividerInsetEndResource (Context context, 
                int insetEndId)

Sets the end inset of the divider.

Related XML Attributes:

Parameters
context Context

insetEndId int: The id of the inset dimension resource to be set.

setDividerInsetStart

public void setDividerInsetStart (int insetStart)

Sets the start inset of the divider.

Related XML Attributes:

Parameters
insetStart int: The start inset to be set.

setDividerInsetStartResource

public void setDividerInsetStartResource (Context context, 
                int insetStartId)

Sets the start inset of the divider.

Related XML Attributes:

Parameters
context Context

insetStartId int: The id of the inset dimension resource to be set.

setDividerThickness

public void setDividerThickness (int thickness)

Sets the thickness of the divider.

Related XML Attributes:

Parameters
thickness int: The thickness value to be set.

setDividerThicknessResource

public void setDividerThicknessResource (Context context, 
                int thicknessId)

Sets the thickness of the divider.

Related XML Attributes:

Parameters
context Context

thicknessId int: 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.

Related XML Attributes:

Parameters
lastItemDecorated boolean: 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 RecyclerView.LayoutManager changes orientation.

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

Parameters
orientation int: 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
position int: the position of the current item being drawn.

adapter Adapter: the RecyclerView.Adapter associated with the item being drawn.

Returns
boolean