androidandroid-layoutandroid-recyclerviewandroid-flexboxlayout

Flexboxlayout Spacing between items programmatically


I've this Flexboxlayout definition (official Google library):

<com.google.android.flexbox.FlexboxLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:dividerDrawableHorizontal="@drawable/flexboxlayout_divider"
    app:flexWrap="wrap"
    app:justifyContent="space_between"
    app:showDividerHorizontal="middle">

Now I want to create the same programmatically:

FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(activity);
flexboxLayoutManager.setFlexDirection(FlexDirection.COLUMN);
flexboxLayoutManager.setJustifyContent(JustifyContent.SPACE_BETWEEN);
flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
myRecyclerView.setLayoutManager(flexboxLayoutManager);

What I'm missing is the app:dividerDrawableHorizontal and app:showDividerHorizontal attributes and didn't find any method to set them.


Solution

  • As it extends from RecyclerView.LayoutManager and RecyclerView.ItemDecoration

    you can just do

    
    FlexboxItemDecoration itemDecoration = new FlexboxItemDecoration(context);
    itemDecoration.setOrientation(FlexboxItemDecoration.HORIZONTAL); // or VERTICAL or BOTH
    itemDecoration.setDrawable(drawable); // replace with actual drawable
    myRecyclerView.addItemDecoration(itemDecoration);