Search code examples
androidrectandroid-recyclerview

Change output Rect color in RecyclerView ItemDecoration - Android


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 ?


Solution

  • 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