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() {
flowLayout.removeAllViews();
for (final Element e : elementsList) {
View viewElement = layoutInflater.inflate(R.layout.elements_item, flowLayout, false);
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());
flowLayout.addView(viewElement );
}
}
, !