我正在使用recyclerViewCategories.setLayoutManager
动态设置列数
代码:
/** Initilize the GridView **/
private void initGridView() {
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getActivity(), 2);
mGridLayoutManager.setSpanSizeLookup(onSpanSizeLookup);
recyclerViewCategories.setLayoutManager(mGridLayoutManager);
recyclerViewCategories.setItemAnimator(new DefaultItemAnimator());
}
GridLayoutManager.SpanSizeLookup onSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return categoryHomeModelList[position].getDisplayType().equals("0") ? 2 : 1;
}
};
答案 0 :(得分:0)
尝试一下
对于RecyclerView
:
<android.support.v7.widget.RecyclerView
android:id="@+id/rcy_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="5dp" /> <== For side padding of list
对于Adapter
,假设您正在使用LinearLayout
,则:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp"> <= This is for each item spacing in list
.....
.....
</LinearLayout>
这将为每个项目提供10dp的间距。无需更改列也可以使用。