Flexboxlayout 项目之间的间距以编程方式

Flexboxlayout Spacing between items programmatically

我有这个 Flexboxlayout 定义 (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">

现在我想以编程方式创建相同的内容:

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

我缺少的是 app:dividerDrawableHorizontalapp:showDividerHorizontal 属性,但没有找到任何方法来设置它们。

因为它从 RecyclerView.LayoutManagerRecyclerView.ItemDecoration

延伸

你可以做到


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