在RecyclerView ItemDecoration中更改输出Rect颜色 - Android

Say*_*san 5 android rect android-recyclerview

我在ItemDecoration类中为recyclerView提供了这个方法 -

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

        // Add top margin only for the first item to avoid double space between items
        if(parent.getChildPosition(view) == 0)
            outRect.top = space;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法改变输出outRect的颜色,如重绘它或什么?

Nic*_*hel 1

你可以看看ItemDecoration 文档

需要了解的 2 个主要事项:

  • getItemOffsets(...)将允许您使用参数中提供的矩形确定项目之间的空间。
  • onDraw(...)将允许您在您设置的空间中绘制任何内容getItemOffsets(...)。您可以使用 aDrawable或简单提供的Canvas等。

如果还不够清楚,请查看这篇文章。我解释如何构建您的定制ItemDecoration