public class DividerItemDecoration extends RecyclerView.ItemDecoration
RecyclerView.ItemDecoration
for RecyclerView to draw
dividers between items. This ItemDecoration will draw the drawable specified by
setDivider(android.graphics.drawable.Drawable)
as the divider in between each item by
default, and the behavior of whether the divider is shown can be customized by subclassing
DividerItemDecoration.DividedViewHolder
.
Modified from v14 PreferenceFragment.DividerDecoration, added with inset capabilities.
Modifier and Type | Class and Description |
---|---|
static interface |
DividerItemDecoration.DividedViewHolder |
static interface |
DividerItemDecoration.DividerCondition |
Modifier and Type | Field and Description |
---|---|
static int |
DIVIDER_CONDITION_BOTH |
static int |
DIVIDER_CONDITION_EITHER |
Constructor and Description |
---|
DividerItemDecoration() |
DividerItemDecoration(Context context) |
Modifier and Type | Method and Description |
---|---|
static DividerItemDecoration |
getDefault(Context context)
Deprecated.
|
Drawable |
getDivider()
Gets the drawable currently used as the divider.
|
int |
getDividerCondition()
Gets whether the divider needs permission from both the item view holder below
and above from where the divider would draw itself or just needs permission from
one or the other before drawing itself.
|
int |
getDividerHeight()
Gets the divider height, in pixels.
|
void |
getItemOffsets(Rect outRect,
View view,
RecyclerView parent,
RecyclerView.State state)
Retrieve any offsets for the given item.
|
protected boolean |
isDividerAllowedAbove(RecyclerView.ViewHolder viewHolder)
Whether a divider is allowed above the view holder.
|
protected boolean |
isDividerAllowedBelow(RecyclerView.ViewHolder viewHolder)
Whether a divider is allowed below the view holder.
|
void |
onDraw(Canvas c,
RecyclerView parent,
RecyclerView.State state)
Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
|
void |
setDivider(Drawable divider)
Sets the drawable to be used as the divider.
|
void |
setDividerCondition(int dividerCondition)
Sets whether the divider needs permission from both the item view holder below
and above from where the divider would draw itself or just needs permission from
one or the other before drawing itself.
|
void |
setDividerHeight(int dividerHeight)
Sets the divider height, in pixels.
|
getItemOffsets, onDraw, onDrawOver, onDrawOver
public static final int DIVIDER_CONDITION_EITHER
public static final int DIVIDER_CONDITION_BOTH
public DividerItemDecoration()
public DividerItemDecoration(Context context)
@Deprecated public static DividerItemDecoration getDefault(Context context)
DividerItemDecoration(android.content.Context)
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
RecyclerView.ItemDecoration
onDraw
in class RecyclerView.ItemDecoration
c
- Canvas to draw intoparent
- RecyclerView this ItemDecoration is drawing intostate
- The current state of RecyclerViewpublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
RecyclerView.ItemDecoration
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.
getItemOffsets
in class RecyclerView.ItemDecoration
outRect
- Rect to receive the output.view
- The child view to decorateparent
- RecyclerView this ItemDecoration is decoratingstate
- The current state of RecyclerView.protected boolean isDividerAllowedAbove(RecyclerView.ViewHolder viewHolder)
getDividerCondition()
. The default implementation delegates to
DividerItemDecoration.DividedViewHolder
, or simply allows
the divider if the view holder doesn't implement DividedViewHolder
. Subclasses can
override this to give more information to decide whether a divider should be drawn.protected boolean isDividerAllowedBelow(RecyclerView.ViewHolder viewHolder)
getDividerCondition()
. The default implementation delegates to
DividerItemDecoration.DividedViewHolder
, or simply allows
the divider if the view holder doesn't implement DividedViewHolder
. Subclasses can
override this to give more information to decide whether a divider should be drawn.public void setDivider(Drawable divider)
public Drawable getDivider()
public void setDividerHeight(int dividerHeight)
public int getDividerHeight()
public void setDividerCondition(int dividerCondition)
public int getDividerCondition()