【问题标题】:Center Columns in RecyclerViewRecyclerView 中的中心列
【发布时间】: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


【解决方案1】:

你可以使用Staggered Grid Layout Manager

作为

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

这里是Link供参考

【讨论】:

  • 嗨,正如我在操作中提到的,我尝试了交错网格布局,但它不起作用,除非我可以包裹内容宽度,否则它不会有任何效果。
【解决方案2】:

在 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 下载 repo

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

【讨论】:

  • 谢谢,但我需要使用回收站视图,如果我能正确包装内容,我认为交错布局会起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
相关资源
最近更新 更多