package com.example.base.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration

/**
 * 设置上下间隔&左右间隔
 */
class SpacesItemDecoration(
    private val space: Int,
    @RecyclerView.Orientation
    private val orientation: Int = RecyclerView.VERTICAL
) : ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        // 第一个ItemView不需要在上面绘制分割线
        if (parent.getChildLayoutPosition(view) != 0) {
            if (orientation == RecyclerView.VERTICAL) {
                outRect.top = space
            } else {
                outRect.left = space
            }
        }
    }
}