Android – the last two items of the recyclview center?

How do I center the last two items of the recyclerview of three columns?

The example has five items:

1 item | 2 item | 3 item
     4 item | 5 item

Image to explain

resolvent:

I encountered the same situation and found something very interesting: flowlayout

I find this library easy to use. Here are the instructions: https://github.com/ApmeM/android-flowlayout. It automatically centralizes elements!

For example, in my case, after adding a library in gradle using the following method: compile 'org. Apmem. Tools: layouts: 1 10@aar ’, I changed the recyclerview element:

<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" />

After the change, I created the method loadelementstotheflowlayout(). Here is:

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 );
    }
}

Hope to help you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>