public class ItemDividerDecoration extends RecyclerViewItemDecoration
RecyclerViewItemDecoration implementation that may be used to draw a divider
(drawable or color), vertically or horizontally, between items displayed in a RecyclerView
widget.
ItemDividerDecoration Attributes
noneRecyclerView.addItemDecoration(RecyclerView.ItemDecoration)| Modifier and Type | Class and Description |
|---|---|
static interface |
ItemDividerDecoration.Orientation
Defines an annotation for determining allowed orientations for
ItemDividerDecoration. |
RecyclerViewItemDecoration.Precondition| Modifier and Type | Field and Description |
|---|---|
static int |
HORIZONTAL
Horizontal orientation flag copied from
LinearLayout.HORIZONTAL for convenience. |
static int |
VERTICAL
Vertical orientation flag copied from
LinearLayout.VERTICAL for convenience. |
| Constructor and Description |
|---|
ItemDividerDecoration()
Same as
ItemDividerDecoration(Context) with null context. |
ItemDividerDecoration(Context context)
Same as
ItemDividerDecoration(Context, AttributeSet) with null attrs. |
ItemDividerDecoration(Context context,
AttributeSet attrs)
Same as
ItemDividerDecoration(Context, AttributeSet, int) with 0 defStyleAttr. |
ItemDividerDecoration(Context context,
AttributeSet attrs,
int defStyleAttr)
Same as
ItemDividerDecoration(Context, AttributeSet, int, int) with 0 defStyleRes. |
ItemDividerDecoration(Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes)
Creates a new instance of ItemDividerDecoration for the given context.
|
ItemDividerDecoration(int orientation,
Drawable divider)
Creates a new instance of ItemDividerDecoration with the specified orientation
and divider.
|
| Modifier and Type | Method and Description |
|---|---|
Drawable |
getDivider()
Returns the divider drawable that is drawn by this decoration between items.
|
int |
getDividerOffsetEnd()
Returns the amount by which to offset the divider at the end.
|
int |
getDividerOffsetStart()
Returns the amount by which to offset the divider at the start.
|
int |
getDividerThickness()
Returns the thickness in which should be the divider drawn between items.
|
void |
getItemOffsets(Rect rect,
View view,
RecyclerView parent,
RecyclerView.State state) |
int |
getOrientation()
Returns the orientation in which is the divider drawable drawn between items.
|
void |
onDraw(Canvas canvas,
RecyclerView parent,
RecyclerView.State state) |
protected void |
onDrawHorizontally(Canvas canvas,
RecyclerView parent,
RecyclerView.State state)
Called from
RecyclerView.ItemDecoration.onDrawOver(Canvas, RecyclerView, RecyclerView.State) in order to draw
this decoration in vertical orientation. |
protected void |
onDrawVertically(Canvas canvas,
RecyclerView parent,
RecyclerView.State state)
Called from
RecyclerView.ItemDecoration.onDrawOver(Canvas, RecyclerView, RecyclerView.State) in order to draw
this decoration in horizontal orientation. |
void |
setDivider(Drawable divider)
Sets a drawable of the divider to be drawn between items of the associated
RecyclerView
in the orientation specified for this decoration. |
void |
setDividerOffset(int start,
int end)
Specifies amounts by which to offset the divider.
|
void |
setDividerThickness(int thickness)
Sets a thickness in which should be the divider specified for this decoration drawn.
|
void |
setOrientation(int orientation)
Sets an orientation in which should be the divider specified for this decoration drawn.
|
protected boolean |
shouldDecorate(RecyclerView parent,
RecyclerView.State state)
Checks whether any subsequent decoration algorithm should be applied by this decoration for
the given parent RecyclerView and its current state.
|
protected void |
updateItemOffsets(Rect rect,
boolean rtlDirection)
Called to update the given rect with the current divider thickness and divider offsets
specified for this decoration according to the orientation also specified for this decoration.
|
getPrecondition, setPrecondition, setSkipFirst, setSkipLast, skipsFirst, skipsLastgetItemOffsets, onDraw, onDrawOver, onDrawOverpublic static final int HORIZONTAL
LinearLayout.HORIZONTAL for convenience.public static final int VERTICAL
LinearLayout.VERTICAL for convenience.public ItemDividerDecoration()
ItemDividerDecoration(Context) with null context.public ItemDividerDecoration(int orientation,
@NonNull
Drawable divider)
orientation - The desired orientation in which should be the divider drawn. One of
orientation defined by @Orientation annotation.divider - Drawable of the desired divider to be drawn. If this is a ColorDrawable
the divider's thickness need to be specified via setDividerThickness(int)
as the color drawable does not have its intrinsic dimensions specified.setOrientation(int),
setDivider(Drawable)public ItemDividerDecoration(@Nullable Context context)
ItemDividerDecoration(Context, AttributeSet) with null attrs.public ItemDividerDecoration(@Nullable Context context, @Nullable AttributeSet attrs)
ItemDividerDecoration(Context, AttributeSet, int) with 0 defStyleAttr.public ItemDividerDecoration(@Nullable Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)
ItemDividerDecoration(Context, AttributeSet, int, int) with 0 defStyleRes.public ItemDividerDecoration(@Nullable Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)
context - Context in which will be the new decoration presented.attrs - Set of Xml attributes used to configure the new instance of this decoration.defStyleAttr - An attribute which contains a reference to a default style resource for
this decoration within a theme of the given context.defStyleRes - Resource id of the default style for the new decoration.public void setOrientation(int orientation)
Default value: VERTICAL
orientation - The desired orientation. Should be one of orientations defined by
@Orientation annotation.getOrientation()public int getOrientation()
setOrientation(int)public void setDivider(@Nullable Drawable divider)
RecyclerView
in the orientation specified for this decoration.
Note that calling this method will reset any thickness value specified via
setDividerThickness(int) before and the thickness of the given divider drawable will
be used instead depending on the orientation specified for this decoration.
divider - The desired divider drawable. May be null to to not draw any divider.getDivider()@Nullable public Drawable getDivider()
null if no divider has been specified.setDivider(Drawable)public void setDividerThickness(int thickness)
Note that calling this method will override the intrinsic thickness obtained form the divider
drawable when setDivider(Drawable) has been called.
thickness - The desired thickness. May be 0 to not draw the divider.getDividerThickness()public int getDividerThickness()
setDividerThickness(int)public void setDividerOffset(int start,
int end)
Both values are used with respect to layout direction of the parent RecyclerView and
also with respect to the orientation specified for this decoration.
start - The desired amount in pixels by which to offset the divider at the start.end - The desired amount in pixels by which to offset the divider at the end.getDividerOffsetStart(),
getDividerOffsetEnd()public int getDividerOffsetStart()
This value is used with respect to layout direction of the parent RecyclerView and
also with respect to the orientation specified for this decoration.
setDividerOffset(int, int)public int getDividerOffsetEnd()
This value is used with respect to layout direction of the parent RecyclerView and
also with respect to the orientation specified for this decoration.
setDividerOffset(int, int)public void getItemOffsets(@NonNull Rect rect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state)
getItemOffsets in class RecyclerView.ItemDecorationprotected void updateItemOffsets(@NonNull Rect rect, boolean rtlDirection)
rect - The desired item offsets rect to be updated.rtlDirection - True if offsets should be updated for RTL layout direction,
false for LTR layout direction.public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state)
onDraw in class RecyclerView.ItemDecorationprotected boolean shouldDecorate(@NonNull RecyclerView parent, @NonNull RecyclerView.State state)
RecyclerViewItemDecoration
This implementation checks whether the given RecyclerView has its RecyclerView.LayoutManager
specified by RecyclerView.getLayoutManager() != null and if there are any items to be
decorated by RecyclerView.State.getItemCount() > 0. If both conditions are met, this
method returns true, if not, false is returned.
shouldDecorate in class RecyclerViewItemDecorationparent - The RecyclerView into which is this decoration added.state - Current state of the parent RecyclerView.True if decorating should be performed, false otherwise.protected void onDrawHorizontally(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state)
RecyclerView.ItemDecoration.onDrawOver(Canvas, RecyclerView, RecyclerView.State) in order to draw
this decoration in vertical orientation.canvas - Canvas on which to draw.parent - RecyclerView into which is this decoration added.state - Current state of the parent RecyclerView.protected void onDrawVertically(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state)
RecyclerView.ItemDecoration.onDrawOver(Canvas, RecyclerView, RecyclerView.State) in order to draw
this decoration in horizontal orientation.canvas - Canvas on which to draw.parent - RecyclerView into which is this decoration added.state - Current state of the parent RecyclerView.