package com.remax.visualnovel.widget.itemdecoration

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

//定义2个Item之间的距离
class VerticalItemDecoration(
    private val space: Int,
    private val topSpace: Int = 0,
    private val bottomSpace: Int = 0,
    private val headerCount: Int = 0
) : ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val position = parent.getChildAdapterPosition(view)
        if (position < headerCount) {
            return
        }
        val totalCount = parent.adapter!!.itemCount
        when (position) {
            0 -> { //第一个
                outRect.top = topSpace
                outRect.bottom = space / 2
            }

            totalCount - 1 -> { //最后一个
                outRect.top = space / 2
                outRect.bottom = bottomSpace
            }

            else -> { //中间其它的
                outRect.top = space / 2
                outRect.bottom = space / 2
            }
        }
    }
}