Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – RecyclerView中的中心列大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个动态的RecyclerView布局.我使用的GridLayoutManager有三列.

我需要包装每个网格项并使列居中(如附加).

我尝试了一个StaggeredGridLayout,我尝试了WrapGridLayoutManager,但这些都没有奏效.

这是我的RecyclerView:

<android.support.v7.widget.RecyclerView
        xmlns:android="http://scheR_196_11845@as.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://scheR_196_11845@as.android.com/apk/res/android"
    android:orientation="horizontal"
    android:BACkground="@drawable/first_time_category_un@R_616_10288@cted"
    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);
            parent.setCliPTOPadding(false);
        }

        outRect.top = halfSpace;
        outRect.bottom = halfSpace;
        outRect.left = halfSpace;
        outRect.right = halfSpace;
    }
}

这就是我想要实现的目标:
@L_772_7@

这就是我得到的:

android – RecyclerView中的中心列


如果有人可以提供帮助,我会非常感激

解决方法

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

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

这是Link供参

@H_944_49@

大佬总结

以上是大佬教程为你收集整理的android – RecyclerView中的中心列全部内容,希望文章能够帮你解决android – RecyclerView中的中心列所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。