在 GridLayout RecyclerView 中添加分隔线 [英] Adding dividers in GridLayout RecyclerView

查看:68
本文介绍了在 GridLayout RecyclerView 中添加分隔线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将水平分隔线添加到我的 GridLayout RecyclerView.但由于某种原因,分隔线没有出现.

I tried adding horizontal dividers to my GridLayout RecyclerView. But for some reason, the dividers didn't show up.

作为替代方案,我使用了带有 2 个参数的 addItemDecoration 方法,其中第二个参数是在其后添加 ItemDecoration 的索引.

RecyclerView r = (RecyclerView) findViewById(R.id.recycler_view);
r.setAdapter(new AppAdapter(this, fetchData());
r.setLayoutManager(new GridLayoutManager(this, 5));
r.addItemDecoration(new DividerItemDecoration(this, GridLayoutManager.HORIZONTAL), 2);

它强制关闭,但有以下异常:

Caused by: java.lang.IndexOutOfBoundsException: Index: 2, Size: 0

这个尺寸是什么意思?为什么为零?

可能是因为我在 Activity 而不是 Fragment 中使用了这个 RecyclerView?

Could it be because I am using this RecyclerView in an Activity instead of a Fragment?

P.S.:适配器没有问题;它不是空的.

P.S.: There is no issue with the adapter; it is not empty.

推荐答案

错误是因为您告诉 RecyclerView 绘制装饰数字 2,而没有告诉它绘制数字 1 和 0.

The error is because you are telling the RecyclerView to draw your decoration number 2, without telling it what to draw number 1 and 0.

(它被实现为要按顺序绘制的装饰的 ArrayList,它试图将索引 2 处的装饰插入一个空的 ArrayList - 因此是 OutOfBounds!)

(It's implemented as an ArrayList of decorations to draw in order, and it's trying to insert your decoration at index 2 into an empty ArrayList - hence OutOfBounds!)

这是一个工作示例:

我已经测试了这段代码,如果您进行以下更改,它可以正常工作:

I've tested this code, and it works fine if you make the following changes:

1- 使用方法的单参数版本,索引版本将超出说明范围(您需要从 0 开始索引).

1- Use the single parameter version of the method, the indexed version will give out of bounds as explained (you need to start indexing from 0).

r.addItemDecoration(new SpacesItemDecoration(10));

2- 使用以下 ItemDecoration 类:

2- Use the following ItemDecoration class:

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int space;

    public SpacesItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.bottom = space;
    }
}

这只会在底部为您提供空间(用于水平分隔线).

This will give you space only at the bottom (for horizontal dividers).

3 - 更改 RecyclerView 布局,使 RecyclerView 的背景颜色是您希望分隔线的颜色:

3 - Change your RecyclerView layout so that the background colour of your RecyclerView is the colour you want the dividers to be:

<android.support.v7.widget.RecyclerView
                                                    android:layout_width="match_parent"
                                                    android:layout_height="match_parent"
                                                    android:background="#000000"> # DIVIDER COLOUR

            </android.support.v7.widget.RecyclerView>

4- 更改您的项目布局,使根项目的背景不透明,并且与 RecyclerView 的颜色不同(显然)

4- Change your item layout so that the background of the root item is not transparent, and not the same colour as the RecyclerView (obviously)

<RelativeLayout
            android:id="@+id/item_root"
            android:background="#cc00cc"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

之前没有显示的原因可能是因为 RecyclerView 背景和项目背景相同,所以分隔线不可见.

The reason nothing was showing before may have been because the RecyclerView background and the item background were the same, so the divider wasn't visible.

此方法适用于线性和网格布局管理器.

This method works with both Linear and Grid Layout Managers.

我希望这(终于!)有帮助:)

I hope this (finally!) helps :)

这篇关于在 GridLayout RecyclerView 中添加分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆