Skip to content

5.x | Flexible Item Decoration

Davide Steduto edited this page May 1, 2018 · 11 revisions

In this page


Features

This item decorator manages custom space offsets and any dividers between items.

  • It supports all RecyclerView LayoutManagers that implement Linear, Grid or StaggeredGrid;
  • Applies equal space offset to all sides of an item, edges are optional;
  • Recognizes the sections of FlexibleAdapter to add gap offset between them;
  • Recognizes the orientation (VERTICAL or HORIZONTAL) of the current Layout;
  • For dividers it implements identical drawing technique of DividerItemDecorator from Android API;

ℹ️ Note: Dragging/moving an item has already an automatic update of existing offsets, in all others use cases (such as orientation change) you are responsible to invalidate the item decorations by calling the method FlexibleAdapter#invalidateItemDecorations(delay) which post-delay the recalculation of the offsets due to any layout change that modifies the order of the items. The delay gives time to the LayoutManager to complete the new layout.

Class location 👓

eu.davidea.flexibleadapter.common
  |_ FlexibleItemDecoration

Instantiation

mRecyclerView.addItemDecoration(new FlexibleItemDecoration(Context)
    .addItemViewType(...)    // for offset only
    .removeItemViewType(...) // for offset only
    .with...()); //see below

Use with Offset

The types of Offsets

  • You can apply a general item offset that will affect all the view types:
.withOffset(int offset) //value in dpi
  • You can apply a section gap offset displayed only at the end of each section (it works only with LinearLayout!):
.withSectionGapOffset(int sectionOffset) //value in dpi
  • Each viewType can have specific offset applied:
.addItemViewType(@LayoutRes int viewType, int horizontalOffset, int verticalOffset)
.removeItemViewType(@LayoutRes int viewType) //removes the offset configuration if needed

For VERTICAL orientation, the horizontalOffset will affect the top and the bottom offsets, while the verticalOffset will affect the left and the right offsets.

ℹ️ Note:

  • The offset of the IHeader items will be retained as margin for the sticky layout!
  • Unspecified viewType will have 0 offset and the item will be attached to the edges of the RecyclerView, unless a general offset and edges are set!
  • You can combine all types.

Edges

All the previous configurations may have optional edge offset. All combinations are possible. So, you can customize the offset to be displayed or not at the 4 edges: first and last column / first and last row. By default all edges are disabled.

// Same offset to all 4 edges
.withEdges(true)      //all around

// Individual offset
.withLeftEdge(true)   //first column
.withTopEdge(true)    //first row
.withRightEdge(true)  //last column
.withBottomEdge(true) //last row

Examples

Edges SettingsEdges

No edges SettingsNo edges

ℹ️ Note: In the example above, the color is the RecyclerView background.

Use with Divider

  • The Android divider android.R.attr.listDivider will be drawn.
.withDefaultDivider([Integer... viewTypes])
  • Custom divider can be specified by a DrawableRes id.
.withDivider(@DrawableRes int resId[, Integer... viewTypes])
  • Drawing the divider can be done over or underneath the items. By default, divider will be drawn underneath the items.
.withDrawOver(boolean drawOver)

Examples

No edges Settings

Clone this wiki locally