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

Jum*_*mpa 4 android android-layout android-recyclerview android-flexboxlayout

我有这个 Flexboxlayout 定义(官方 Google 库):

<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">
Run Code Online (Sandbox Code Playgroud)

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

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

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

And*_*rew 6

由于它从RecyclerView.LayoutManager和延伸RecyclerView.ItemDecoration

你可以这样做


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

Run Code Online (Sandbox Code Playgroud)