-
public class FlexibleItemDecoration extends RecyclerView.ItemDecoration
This item decorator implements identical drawing technique of
{@code DividerItemDecorator}from Android API, and at the same time, it adds several useful functionalities:- Supports all RecyclerView LayoutManagers that implement (Linear, Grid or StaggeredGrid).
- Can add equal space offset at all sides of an item.
- Can customize the offset for each view type.
- Can customize the offset at the 4 edges: first and last column / first and last row.
- Recognizes the sections of FlexibleAdapter and can add gap offset between them.
- Recognizes the orientation of the current Layout.
- Supports the default Android divider
{@code android.R.attr.listDivider}. - Supports a custom divider by
{@code DrawableRes id}. - Supports drawing the divider over or underneath the items.
- Supports drawing the divider for specific viewTypes.
-
-
Constructor Summary
Constructors Constructor Description FlexibleItemDecoration(Context context)Constructor which saves the Context to calculate the dpi OR to retrieve the divider later on.
-
Method Summary
Modifier and Type Method Description FlexibleItemDecorationwithDefaultDivider(Array<Integer> viewTypes)Default Android divider will be used. FlexibleItemDecorationwithDivider(@DrawableRes() int resId, Array<Integer> viewTypes)Custom divider. FlexibleItemDecorationwithDrawDividerOnLastItem(boolean lastItem)Allows to draw the Divider after the last item. FlexibleItemDecorationremoveDivider()Removes any divider previously set. FlexibleItemDecorationwithDrawOver(boolean drawOver)Changes the mode to draw the divider. voidonDraw(Canvas c, RecyclerView parent, RecyclerView.State state)voidonDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)FlexibleItemDecorationwithSectionGapOffset(@IntRange(from = 0) int sectionOffset)Adds an extra offset at the end of each section. FlexibleItemDecorationwithSectionGapOnLastItem(boolean lastItem)Allows to disable the SectionGap after the last item. FlexibleItemDecorationwithEdge(boolean withEdge)Adds an offset to the items at the 4 edges (left, top, right and bottom). FlexibleItemDecorationwithLeftEdge(boolean withLeftEdge)Adds an offset to the items at the Left edge. FlexibleItemDecorationwithTopEdge(boolean withTopEdge)Adds an offset to the items at the Top edge. FlexibleItemDecorationwithBottomEdge(boolean withBottomEdge)Adds an offset to the items at the Bottom edge. FlexibleItemDecorationwithRightEdge(boolean withRightEdge)Adds an offset to the items at the Right edge. intgetOffset()Returns the current general offset in dpi. FlexibleItemDecorationwithOffset(@IntRange(from = 0) int offset)Applies the same physical offset to all sides of the item AND between items. FlexibleItemDecorationaddItemViewType(@LayoutRes() int viewType)Applies the general offset also to the specified viewType. FlexibleItemDecorationaddItemViewType(@LayoutRes() int viewType, int offset)As addItemViewType but with custom offset equals to all sides that willaffect only this viewType. FlexibleItemDecorationaddItemViewType(@LayoutRes() int viewType, int left, int top, int right, int bottom)As addItemViewType but with custom offset that will affect only this viewType. FlexibleItemDecorationremoveItemViewType(@LayoutRes() int viewType)In case a viewType should not have anymore the applied offset. voidgetItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state)-
-
Constructor Detail
-
FlexibleItemDecoration
FlexibleItemDecoration(Context context)
Constructor which saves the Context to calculate the dpi OR to retrieve the divider later on.- Parameters:
context- current Context, it will be used to calculate dpi OR to retrieve the divider
-
-
Method Detail
-
withDefaultDivider
FlexibleItemDecoration withDefaultDivider(Array<Integer> viewTypes)
Default Android divider will be used.
- Parameters:
viewTypes- the specific ViewTypes for which the divider will be drawn OR do notspecify any to draw divider for all ViewTypes (default behavior).
-
withDivider
FlexibleItemDecoration withDivider(@DrawableRes() int resId, Array<Integer> viewTypes)
Custom divider.
- Parameters:
resId- drawable resourceId that should be used as a dividerviewTypes- the specific ViewTypes for which the divider will be drawn OR do notspecify any to draw divider for all ViewTypes (default behavior).
-
withDrawDividerOnLastItem
FlexibleItemDecoration withDrawDividerOnLastItem(boolean lastItem)
Allows to draw the Divider after the last item.
Default is
{@code false}(don't draw).- Parameters:
lastItem-{@code true}to draw,{@code false}to not draw
-
removeDivider
FlexibleItemDecoration removeDivider()
Removes any divider previously set.
-
withDrawOver
FlexibleItemDecoration withDrawOver(boolean drawOver)
Changes the mode to draw the divider.
- When
Default value is false (drawn underneath).{@code false}, any content will be drawn before the item views are drawn, and willthus appear underneath the views.- When{@code true}, any content will be drawn after the item views are drawn, and willthus appear over the views.- Parameters:
drawOver- true to draw after the item has been added, false to draw underneath the item
-
onDraw
void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
-
onDrawOver
void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
-
withSectionGapOffset
FlexibleItemDecoration withSectionGapOffset(@IntRange(from = 0) int sectionOffset)
Adds an extra offset at the end of each section.
Works only with FlexibleAdapter.
- Parameters:
sectionOffset- the extra offset at the end of each section
-
withSectionGapOnLastItem
FlexibleItemDecoration withSectionGapOnLastItem(boolean lastItem)
Allows to disable the SectionGap after the last item.
Default is
{@code true}(draw).- Parameters:
lastItem-{@code true}to draw,{@code false}to not draw
-
withEdge
FlexibleItemDecoration withEdge(boolean withEdge)
Adds an offset to the items at the 4 edges (left, top, right and bottom).
Note: An offset OR an itemType with offsets must be added.
Default value is{@code false}(no edge).- Parameters:
withEdge- true to enable, false otherwise
-
withLeftEdge
FlexibleItemDecoration withLeftEdge(boolean withLeftEdge)
Adds an offset to the items at the Left edge.
Note: An offset OR an itemType with offsets must be added.
Default value is{@code false}(no edge).- Parameters:
withLeftEdge- true to add offset to the items at the Left of the first column.
-
withTopEdge
FlexibleItemDecoration withTopEdge(boolean withTopEdge)
Adds an offset to the items at the Top edge.
Note: An offset OR an itemType with offsets must be added.
Default value is{@code false}(no edge).- Parameters:
withTopEdge- true to add offset to the items at the Top of the first row.
-
withBottomEdge
FlexibleItemDecoration withBottomEdge(boolean withBottomEdge)
Adds an offset to the items at the Bottom edge.
Note: An offset OR an itemType with offsets must be added.
Default value is{@code false}(no edge).- Parameters:
withBottomEdge- true to add offset to the items at the Bottom of the last row.
-
withRightEdge
FlexibleItemDecoration withRightEdge(boolean withRightEdge)
Adds an offset to the items at the Right edge.
Note: An offset OR an itemType with offsets must be added.
Default value is{@code false}(no edge).- Parameters:
withRightEdge- true to add offset to the items at the Right of the last column.
-
getOffset
int getOffset()
Returns the current general offset in dpi.
-
withOffset
FlexibleItemDecoration withOffset(@IntRange(from = 0) int offset)
Applies the same physical offset to all sides of the item AND between items.
- Parameters:
offset- the offset in dpi to apply
-
addItemViewType
FlexibleItemDecoration addItemViewType(@LayoutRes() int viewType)
Applies the general offset also to the specified viewType.
Call withOffset to set a general offset.
- Parameters:
viewType- the viewType affected
-
addItemViewType
FlexibleItemDecoration addItemViewType(@LayoutRes() int viewType, int offset)
As addItemViewType but with custom offset equals to all sides that willaffect only this viewType.
- Parameters:
viewType- the viewType affected
-
addItemViewType
FlexibleItemDecoration addItemViewType(@LayoutRes() int viewType, int left, int top, int right, int bottom)
As addItemViewType but with custom offset that will affect only this viewType.
- Parameters:
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 item
-
removeItemViewType
FlexibleItemDecoration removeItemViewType(@LayoutRes() int viewType)
In case a viewType should not have anymore the applied offset.
- Parameters:
viewType- the viewType to remove from the decoration management
-
getItemOffsets
void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state)
-
-
-
-