Package 

Class FlexibleItemDecoration


  • 
    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.
    Tip: Call the method invalidateItemDecorations 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.
    • 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 divider
        viewTypes - 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
      • withDrawOver

         FlexibleItemDecoration withDrawOver(boolean drawOver)

        Changes the mode to draw the divider.

        - When {@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.

        Default value is false (drawn underneath).
        Parameters:
        drawOver - true to draw after the item has been added, false to draw underneath the item
      • 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, int left, int top, int right, int bottom)

        As addItemViewType but with custom offset that will affect only this viewType.

        Parameters:
        viewType - the viewType affected
        left - the offset to the left of the item
        top - the offset to the top of the item
        right - the offset to the right of the item
        bottom - the offset to the bottom of the item