RecyclerView中的中心列

时间:2015-11-09 11:09:14

标签: android android-recyclerview gridlayoutmanager

我正在尝试创建动态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 如果有人可以帮忙,我会非常感激

2 个答案:

答案 0 :(得分:0)

您可以使用Staggered Grid Layout Manager

作为

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

以下是Link供参考

答案 1 :(得分:0)

在recyclerView中使用此功能,您无法按照显示的方式进行安排

<cuneyt.example.com.tagview.Tag.TagView
        android:id="@+id/tag_group"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp" />
  

从GIT https://github.com/Cutta/TagView

下载回购
TagView tagGroup = (TagView)findviewById(R.id.tag_view);
//You can add one tag
tagGroup.addTag(Tag tag);
//You can add multiple tag via ArrayList
tagGroup.addTags(ArrayList tags);
//Via string array
 addTags(String[] tags);


//set click listener
  tagGroup.setOnTagClickListener(new OnTagClickListener() {
        @Override
        public void onTagClick(Tag tag, int position) {
        }
    });



//set delete listener
        tagGroup.setOnTagDeleteListener(new OnTagDeleteListener() {
        @Override
        public void onTagDeleted(final TagView view, final Tag tag, final int position) {
        }
    });

How do I make WRAP_CONTENT work on a RecyclerView