Center the last two points of recyclview?

How to center the last two elements RecyclerViewwith three columns ?.

Example with 5 elements:

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

Image for explanation Image for explanation

+4
source share
2 answers

I was in the same situation, and I found something very interesting: FlowLayout.

I found this library and it is very easy to use, here are the instructions: https://github.com/ApmeM/android-flowlayout . It centers the elements automatically!

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

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

, !

+3

GridLayoutManager RecyclerView. setSpanSizeLookup() RecyclerView. getSpanSize(int position).

- :

int lastRowIndex = Math.ceil(yourList.length / numberOfColumns);

GridLayoutManager manager = new GridLayoutManager(context, 2); // MAX NUMBER OF SPACES
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        if (position == lastRowIndex)
            return 2;
        else
            return 3;    
    }
});
0

Source: https://habr.com/ru/post/1664660/


All Articles