答案 0 :(得分: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" />
更改后,我创建了方法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 );
}
}
希望它有所帮助!
答案 1 :(得分:0)
您可以在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;
}
});