其他分享
首页 > 其他分享> > android – RecyclerView中的中心列

android – RecyclerView中的中心列

作者:互联网

我正在尝试创建一个动态的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;
    }
}

这就是我想要实现的目标:
what I'm trying to achieve

这就是我得到的:
enter image description here
如果有人可以提供帮助,我会非常感激

解决方法:

您可以使用交错网格布局管理器

  StagaggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.HORIZONTAL );
        recyclerView.setLayoutManager(gaggeredGridLayoutManager);

这是Link供参考

标签:android,android-recyclerview,gridlayoutmanager
来源: https://codeday.me/bug/20190711/1432945.html