public class DividerItemDecoration extends RecyclerView.ItemDecoration
RecyclerView.ItemDecoration
that can be used as a divider
between items of a LinearLayoutManager
. It supports both HORIZONTAL
and
VERTICAL
orientations.
mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), mLayoutManager.getOrientation()); recyclerView.addItemDecoration(mDividerItemDecoration);
Modifier and Type | Field and Description |
---|---|
static int |
HORIZONTAL |
static int |
VERTICAL |
Constructor and Description |
---|
DividerItemDecoration(Context context,
int orientation)
Creates a divider
RecyclerView.ItemDecoration that can be used with a
LinearLayoutManager . |
Modifier and Type | Method and Description |
---|---|
void |
getItemOffsets(Rect outRect,
View view,
RecyclerView parent,
RecyclerView.State state)
Retrieve any offsets for the given item.
|
void |
onDraw(Canvas c,
RecyclerView parent,
RecyclerView.State state)
Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
|
void |
setDrawable(Drawable drawable)
Sets the
Drawable for this divider. |
void |
setOrientation(int orientation)
Sets the orientation for this divider.
|
getItemOffsets, onDraw, onDrawOver, onDrawOver
public static final int HORIZONTAL
public static final int VERTICAL
public DividerItemDecoration(Context context, int orientation)
RecyclerView.ItemDecoration
that can be used with a
LinearLayoutManager
.context
- Current context, it will be used to access resources.orientation
- Divider orientation. Should be HORIZONTAL
or VERTICAL
.public void setOrientation(int orientation)
RecyclerView.LayoutManager
changes orientation.orientation
- HORIZONTAL
or VERTICAL
public void setDrawable(@NonNull Drawable drawable)
Drawable
for this divider.drawable
- Drawable that should be used as a divider.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.