Change output Rect color in RecyclerView ItemDecoration - Android
Asked Answered
S

1

6

I have this method in ItemDecoration class for 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;
}

Is there any way to change the color of output outRect like redrawing it or something ?

Shanley answered 13/5, 2015 at 13:19 Comment(4)
@pskink how do i use onDraw method here. It would require rect size etc. right ?Shanley
what if view.setBackground()?Canova
did you read ItemDecoration javadocs?Solecism
@Canova view.setBackground() sets color only to items, not to offsetMountain
B
1

You could have a look at ItemDecoration documentation.

2 main things to know :

  • getItemOffsets(...) will allow you to determine the space between your items with provided rect in params.
  • onDraw(...) will allow you to draw whatever you can in that space you set in getItemOffsets(...). You can use a Drawable or simple provided Canvas, etc.

If it's still not clear enough, check this article. I explain how to build your custom ItemDecoration

Begay answered 27/5, 2020 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.