1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import android.content.res.Resources import android.graphics.Rect import android.util.TypedValue import android.view.View import androidx.recyclerview.widget.RecyclerView
class SimplePaddingDecoration( spaceDp: Int, val orientation: Int = RecyclerView.VERTICAL ) : RecyclerView.ItemDecoration() { private val dividerHeight: Int = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, spaceDp.toFloat(), Resources.getSystem().displayMetrics ).toInt()
override fun getItemOffsets( outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State ) { val position = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
if (orientation == RecyclerView.VERTICAL) { if (position + 1 != parent.adapter?.itemCount) { outRect.set(0, 0, 0, dividerHeight) } else { outRect.set(0, 0, 0, 0) } } else { if (position + 1 != parent.adapter?.itemCount) { outRect.set(0, 0, dividerHeight, 0) } else { outRect.set(0, 0, 0, 0) } } } }
|