Android – central column in recyclerview

I'm trying to create a dynamic recyclerview layout. The gridlayoutmanager I use has three columns

I need to wrap each grid item and center the column (if attached)

I tried a staggeredgridlayout, I tried wrapgridlayoutmanager, but none of these worked

This is my 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>

And my recyclerview project:

 <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>

This is the decoration I use to achieve grid spacing:

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;
    }
}

This is what I want to achieve:

That's what I got:

resolvent:

You can use the staggered grid layout manager

as

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

This is link for reference

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>