Created
May 30, 2025 22:40
-
-
Save devapro/86ab013ff40b213f9c7a0dcdf7d8e62e to your computer and use it in GitHub Desktop.
RecyclerView item decorator example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.graphics.Canvas | |
| import android.graphics.drawable.Drawable | |
| import android.view.View | |
| import androidx.recyclerview.widget.RecyclerView | |
| import androidx.recyclerview.widget.RecyclerView.ItemDecoration | |
| /** | |
| * Divider that doesn't show the divider after the last item. | |
| */ | |
| class InnerDividerItemDecorator( | |
| private val mDivider: Drawable, | |
| private val mDelegate: Delegate? = null | |
| ) : ItemDecoration() { | |
| override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) { | |
| val dividerLeft = parent.paddingLeft | |
| val dividerRight = parent.width - parent.paddingRight | |
| val childCount = parent.childCount | |
| val numItemsFromEndWithoutDivider = mDelegate?.numItemsFromEndWithoutDivider ?: 1 | |
| for (i in 0..(childCount - 1 - numItemsFromEndWithoutDivider)) { | |
| val child: View = parent.getChildAt(i) | |
| val params = child.layoutParams as RecyclerView.LayoutParams | |
| val dividerTop: Int = child.bottom + params.bottomMargin | |
| val dividerBottom = dividerTop + mDivider.intrinsicHeight | |
| mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom) | |
| mDivider.draw(canvas) | |
| } | |
| } | |
| interface Delegate { | |
| val numItemsFromEndWithoutDivider: Int | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment