【发布时间】:2015-11-09 11:09:13
【问题描述】:
我正在尝试创建动态 RecyclerView 布局。我正在使用带有三列的GridLayoutManager。
我需要包装每个网格项并将列居中(如附件)。
我尝试了StaggeredGridLayout,我尝试了WrapGridLayoutManager,但这些都不起作用。
这是我的RecyclerView:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/categories_grid"
android:layout_marginTop="@dimen/feed_item_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
还有我的RecyclerView 物品:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@drawable/first_time_category_unselected"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/category_name"
android:layout_marginLeft="@dimen/feed_item_margin"
android:layout_gravity="center_vertical"
android:textColor="@color/black_colour"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/category_status_icon"
android:layout_gravity="center_vertical"
android:background="@drawable/icon_follow"
android:layout_marginLeft="@dimen/feed_item_margin"
android:layout_marginRight="@dimen/feed_item_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这是我用来实现网格间距的装饰:
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int halfSpace;
public SpacesItemDecoration(int space) {
this.halfSpace = space / 2;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getPaddingLeft() != halfSpace) {
parent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace);
parent.setClipToPadding(false);
}
outRect.top = halfSpace;
outRect.bottom = halfSpace;
outRect.left = halfSpace;
outRect.right = halfSpace;
}
}
【问题讨论】:
-
希望实现类似的目标,您有没有设法让它正常工作?
-
也在尝试这样做。我使用了 gridlayout 管理器并弄乱了 setSpanSize 但后来事情变得均匀分布,只有 2 列。我希望它更紧密地结合在一起,就像你展示的那样。所以互联网上没有人能够解决这个问题。
标签: android android-recyclerview gridlayoutmanager