如何在recyclerview中的项目之间有不同的间距

问题描述 投票:0回答:2

我有一个管理间距的UiItemDecoration类。

public class UiItemDecoration extends RecyclerView.ItemDecoration {

private int spanCount;//一行item数目
private int spacing;//item 之间的间隔
private boolean includeEdge;//边缘
private boolean includeTop;

public UiItemDecoration(int spanCount, int spacing, boolean includeEdge) {
    this.spanCount = spanCount;
    this.spacing = spacing;
    this.includeEdge = includeEdge;
    this.includeTop = true;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view); // item position
    outRect.left = 0;
    outRect.right = 10;
}

当我想要有相同的间距时它很有效。在enter image description here下面的图片中显示问题是我想在类中定义的recyclerview的项目之间有不同的间距:

public class UiItemDecoration extends RecyclerView.ItemDecoration {

private int spanCount;//一行item数目
private int spacing;//item 之间的间隔
private boolean includeEdge;//边缘
private boolean includeTop;

public UiItemDecoration(int spanCount, int spacing, boolean includeEdge) {
    this.spanCount = spanCount;
    this.spacing = spacing;
    this.includeEdge = includeEdge;
    this.includeTop = true;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view); // item position
    if (position%5 == 2){
        outRect.right = 30;
    }else {
        outRect.right = 5;
    }
    outRect.left = 0;
}

但问题是位置2的项目正在缩小。结果如下:enter image description here问题:如何设置recyclerview项目之间的间距

java android
2个回答
0
投票

您可以根据适配器的onBindViewHolder()方法中的位置更改边距,而不是更改偏移量。如果您使用RelativeLayout创建项目布局,则此代码将起作用。调整边距也不会影响项目的视图边界。

@Override
public void onBindViewHolder(@NonNull final AdapterViewHolder holder, int position) {
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.itemView.getLayoutParams();
    if (position%5 == 2){
        layoutParams.setMargins(0, 0, 30, 0);
    }else {
        layoutParams.setMargins(0, 0, 5, 0);
    }
    holder.itemView.setLayoutParams(layoutParams);
}

0
投票

用这个,

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {


    public static final int SPAN_COUNT = 4;


    private static final int DEFAULT_LEFT_OFFSET = 0;

    private static final int DEFAULT_RIGHT_OFFSET = 0;

    private static final int DEFAULT_TOP_OFFSET = 0;

    private static final int DEFAULT_BOTTOM_OFFSET = 0;


    private int mItemOffset;


    public ItemOffsetDecoration(int itemOffset) {
        mItemOffset = itemOffset;
    }

    /**
     * @brief Method to get offset for item views(child)
     *        and modifying all bounds- top, bottom, left and right for equal spacing.
     * @param outRect : Rect, the number of pixels in which a view should be inserted.
     * @param view : Views (child) which to be modified or decorated.
     * @param parent : Corresponding recycler view where decorations being applied.
     * @param state : Recycler view State state, contains focus, scroll etc info.
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        // column count fixed = 5

        if (position >= 0) {
            int column = position % SPAN_COUNT; // item column

            outRect.left = mItemOffset - column * mItemOffset / SPAN_COUNT;
            outRect.right = (column + 1) * mItemOffset / SPAN_COUNT;

            if (position < SPAN_COUNT) { // top edge
                outRect.top = mItemOffset;
            }
            outRect.bottom = mItemOffset; // item bottom
        } else {
            outRect.left = DEFAULT_LEFT_OFFSET;
            outRect.right = DEFAULT_RIGHT_OFFSET;
            outRect.top = DEFAULT_TOP_OFFSET;
            outRect.bottom = DEFAULT_BOTTOM_OFFSET;
        }
    }
}

我正在使用mItemOffset = -6; //使用自己修改

别忘了

GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), SPAN_COUNT);//columns
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.addItemDecoration(new ItemOffsetDecoration(SPACING));
© www.soinside.com 2019 - 2024. All rights reserved.