public class FlexibleItemDecoration
extends RecyclerView.ItemDecoration
DividerItemDecorator
from Android API, and at the same time, it adds several useful functionalities:
android.R.attr.listDivider.DrawableRes id.FlexibleAdapter#invalidateItemDecorations(long) to rebuild
the invalidated offsets due to the changes coming from events like moveItem or any layout
change that modifies the order of the items.| Modifier and Type | Field and Description |
|---|---|
protected static int[] |
ATTRS |
protected android.graphics.Rect |
mBounds |
protected android.graphics.drawable.Drawable |
mDivider |
protected boolean |
mDrawOver |
| Constructor and Description |
|---|
FlexibleItemDecoration(android.content.Context context)
Constructor which saves the Context to calculate the dpi OR to retrieve the divider later on.
|
| Modifier and Type | Method and Description |
|---|---|
FlexibleItemDecoration |
addItemViewType(int viewType)
Applies the general offset also to the specified viewType.
|
FlexibleItemDecoration |
addItemViewType(int viewType,
int offset)
As
addItemViewType(int) but with custom offset equals to all sides that will
affect only this viewType. |
FlexibleItemDecoration |
addItemViewType(int viewType,
int left,
int top,
int right,
int bottom)
As
addItemViewType(int) but with custom offset that will affect only this viewType. |
protected void |
draw(android.graphics.Canvas c,
RecyclerView parent) |
protected void |
drawHorizontal(android.graphics.Canvas canvas,
RecyclerView parent) |
protected void |
drawVertical(android.graphics.Canvas canvas,
RecyclerView parent) |
void |
getItemOffsets(android.graphics.Rect outRect,
android.view.View view,
RecyclerView recyclerView,
RecyclerView.State state) |
int |
getOffset()
Returns the current general offset in dpi.
|
void |
onDraw(android.graphics.Canvas c,
RecyclerView parent,
RecyclerView.State state) |
void |
onDrawOver(android.graphics.Canvas c,
RecyclerView parent,
RecyclerView.State state) |
FlexibleItemDecoration |
removeDivider()
Removes any divider previously set.
|
FlexibleItemDecoration |
removeItemViewType(int viewType)
In case a viewType should not have anymore the applied offset.
|
protected boolean |
shouldDrawDivider(RecyclerView.ViewHolder viewHolder)
Allows to define for which items the divider should be drawn.
|
FlexibleItemDecoration |
withBottomEdge(boolean withBottomEdge)
Adds an offset to the items at the Bottom edge.
|
FlexibleItemDecoration |
withDefaultDivider(java.lang.Integer... viewTypes)
Default Android divider will be used.
|
FlexibleItemDecoration |
withDivider(int resId,
java.lang.Integer... viewTypes)
Custom divider.
|
FlexibleItemDecoration |
withDrawDividerOnLastItem(boolean lastItem)
Allows to draw the Divider after the last item.
|
FlexibleItemDecoration |
withDrawOver(boolean drawOver)
Changes the mode to draw the divider.
|
FlexibleItemDecoration |
withEdge(boolean withEdge)
Adds an offset to the items at the 4 edges (left, top, right and bottom).
|
FlexibleItemDecoration |
withLeftEdge(boolean withLeftEdge)
Adds an offset to the items at the Left edge.
|
FlexibleItemDecoration |
withOffset(int offset)
Applies the same physical offset to all sides of the item AND between items.
|
FlexibleItemDecoration |
withRightEdge(boolean withRightEdge)
Adds an offset to the items at the Right edge.
|
FlexibleItemDecoration |
withSectionGapOffset(int sectionOffset)
Adds an extra offset at the end of each section.
|
FlexibleItemDecoration |
withSectionGapOnLastItem(boolean lastItem)
Allows to disable the SectionGap after the last item.
|
FlexibleItemDecoration |
withTopEdge(boolean withTopEdge)
Adds an offset to the items at the Top edge.
|
protected android.graphics.drawable.Drawable mDivider
protected final android.graphics.Rect mBounds
protected boolean mDrawOver
protected static final int[] ATTRS
public FlexibleItemDecoration(android.content.Context context)
context - current Context, it will be used to calculate dpi OR to retrieve the dividerpublic FlexibleItemDecoration withDefaultDivider(java.lang.Integer... viewTypes)
viewTypes - the specific ViewTypes for which the divider will be drawn OR do not
specify any to draw divider for all ViewTypes (default behavior).withDivider(int, Integer...),
withDrawOver(boolean),
removeDivider()public FlexibleItemDecoration withDivider(int resId, java.lang.Integer... viewTypes)
resId - drawable resourceId that should be used as a dividerviewTypes - the specific ViewTypes for which the divider will be drawn OR do not
specify any to draw divider for all ViewTypes (default behavior).withDefaultDivider(Integer...),
withDrawOver(boolean),
removeDivider()public FlexibleItemDecoration withDrawDividerOnLastItem(boolean lastItem)
Default is false (don't draw).
lastItem - true to draw, false to not drawpublic FlexibleItemDecoration removeDivider()
public FlexibleItemDecoration withDrawOver(boolean drawOver)
- When false, any content will be drawn before the item views are drawn, and will
thus appear underneath the views.
- When true, any content will be drawn after the item views are drawn, and will
thus appear over the views.
drawOver - true to draw after the item has been added, false to draw underneath the itempublic void onDraw(android.graphics.Canvas c,
RecyclerView parent,
RecyclerView.State state)
public void onDrawOver(android.graphics.Canvas c,
RecyclerView parent,
RecyclerView.State state)
protected void draw(android.graphics.Canvas c,
RecyclerView parent)
protected void drawVertical(android.graphics.Canvas canvas,
RecyclerView parent)
protected void drawHorizontal(android.graphics.Canvas canvas,
RecyclerView parent)
protected boolean shouldDrawDivider(RecyclerView.ViewHolder viewHolder)
Override to define custom logic. By default all viewTypes will have the divider.
viewHolder - the ViewHolder under analysistrue to draw the divider, false to skip the drawingpublic FlexibleItemDecoration withSectionGapOffset(int sectionOffset)
Works only with FlexibleAdapter.
sectionOffset - the extra offset at the end of each sectionpublic FlexibleItemDecoration withSectionGapOnLastItem(boolean lastItem)
Default is true (draw).
lastItem - true to draw, false to not drawpublic FlexibleItemDecoration withEdge(boolean withEdge)
Note: An offset OR an itemType with offsets must be added.
Default value isfalse (no edge).withEdge - true to enable, false otherwisewithOffset(int),
addItemViewType(int, int)public FlexibleItemDecoration withLeftEdge(boolean withLeftEdge)
Note: An offset OR an itemType with offsets must be added.
Default value isfalse (no edge).withLeftEdge - true to add offset to the items at the Left of the first column.withOffset(int),
addItemViewType(int, int)public FlexibleItemDecoration withTopEdge(boolean withTopEdge)
Note: An offset OR an itemType with offsets must be added.
Default value isfalse (no edge).withTopEdge - true to add offset to the items at the Top of the first row.withOffset(int),
addItemViewType(int, int)public FlexibleItemDecoration withBottomEdge(boolean withBottomEdge)
Note: An offset OR an itemType with offsets must be added.
Default value isfalse (no edge).withBottomEdge - true to add offset to the items at the Bottom of the last row.withOffset(int),
addItemViewType(int, int)public FlexibleItemDecoration withRightEdge(boolean withRightEdge)
Note: An offset OR an itemType with offsets must be added.
Default value isfalse (no edge).withRightEdge - true to add offset to the items at the Right of the last column.withOffset(int),
addItemViewType(int, int)public int getOffset()
public FlexibleItemDecoration withOffset(int offset)
offset - the offset in dpi to applypublic FlexibleItemDecoration addItemViewType(int viewType)
Call withOffset(int) to set a general offset.
viewType - the viewType affectedwithOffset(int),
addItemViewType(int, int),
removeItemViewType(int)public FlexibleItemDecoration addItemViewType(int viewType, int offset)
addItemViewType(int) but with custom offset equals to all sides that will
affect only this viewType.viewType - the viewType affectedwithOffset(int),
removeItemViewType(int)public FlexibleItemDecoration addItemViewType(int viewType, int left, int top, int right, int bottom)
addItemViewType(int) but with custom offset that will affect only this viewType.viewType - the viewType affectedleft - the offset to the left of the itemtop - the offset to the top of the itemright - the offset to the right of the itembottom - the offset to the bottom of the itemaddItemViewType(int, int),
removeItemViewType(int)public FlexibleItemDecoration removeItemViewType(int viewType)
viewType - the viewType to remove from the decoration managementpublic void getItemOffsets(android.graphics.Rect outRect,
android.view.View view,
RecyclerView recyclerView,
RecyclerView.State state)