将recyclview的最后两项居中?

And*_*ler 6 android android-layout android-recyclerview

如何将 a 的最后两项RecyclerView与 3 列居中?

包含 5 个项目的示例:

1 item | 2 item | 3 item
     4 item | 5 item
Run Code Online (Sandbox Code Playgroud)

图片说明 图片说明

Jor*_*oll 3

我也遇到了同样的情况,我发现了一些非常有趣的东西:FlowLayout。

我找到了这个库,它非常容易使用,以下是说明: https: //github.com/ApmeM/android-flowlayout。它会自动将元素居中!

例如,就我而言,在使用以下命令在 gradle 中添加库后:compile 'org.apmem.tools:layouts:1.10@aar',我为此更改了 RecyclerView 元素:

<org.apmem.tools.layouts.FlowLayout
    android:id="@+id/lytXXXXX"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layoutDirection="ltr" (Left to right)
    android:orientation="horizontal" />
Run Code Online (Sandbox Code Playgroud)

更改后,我创建了方法 loadElementsToTheFlowLayout()。这里是:

private void loadElementsToTheFlowLayout() {
    // Remove all the FlowLayout elements
    flowLayout.removeAllViews();

    // For each element, I create its view and I add it to the FlowLayout
    for (final Element e : elementsList) {
        // Inflate the e view to the flow layout
        View viewElement = layoutInflater.inflate(R.layout.elements_item, flowLayout, false);

        // Create the element view with its data
        FrameLayout elementView = (FrameLayout) viewElement .findViewById(R.id.lytForElement);

        TextViewFont txtLine0 = (TextViewFont) viewElement .findViewById(R.id.txtLine0);
        txtLine0.setText(e.getLine0());

        TextViewFont txtLine1 = (TextViewFont) viewElement .findViewById(R.id.txtLine1);
        txtLine1.setText(e.getLine1());

        TextViewFont txtLine2 = (TextViewFont) viewElement .findViewById(R.id.txtLine2);
        txtLine2.setText(e.getLine2());

        TextViewFont txtLine3 = (TextViewFont) viewElement .findViewById(R.id.txtLine3);
        txtLine3.setText(e.getLine3());

        // Add the view to the FlowLayout
        flowLayout.addView(viewElement );
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!