Created
December 29, 2020 11:04
-
-
Save Mojtaba-Shafaei/a1fc4964110b8933c285047023e606e5 to your computer and use it in GitHub Desktop.
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.Color | |
| import android.graphics.Rect | |
| import android.graphics.drawable.ColorDrawable | |
| import android.view.View | |
| import androidx.annotation.ColorInt | |
| import androidx.core.view.ViewCompat | |
| import androidx.recyclerview.widget.LinearLayoutManager | |
| import androidx.recyclerview.widget.RecyclerView | |
| import kotlin.math.abs | |
| class DividerItemDecoration : RecyclerView.ItemDecoration() { | |
| private var mDivider: ColorDrawable = object : ColorDrawable(Color.TRANSPARENT) { | |
| override fun getIntrinsicWidth(): Int { | |
| return abs(bounds.right - bounds.left) | |
| } | |
| override fun getIntrinsicHeight(): Int { | |
| return abs(bounds.top - bounds.bottom) | |
| } | |
| } | |
| private var mOrientation = RecyclerView.VERTICAL | |
| companion object { | |
| fun create( | |
| orientation: Int = RecyclerView.VERTICAL, | |
| @ColorInt color: Int = Color.TRANSPARENT, | |
| widthInPix: Int, | |
| heightInPix: Int | |
| ): DividerItemDecoration { | |
| val itemDecoration = DividerItemDecoration() | |
| itemDecoration.mOrientation = orientation | |
| itemDecoration.mDivider.color = color | |
| itemDecoration.setDividerSize(0, 0, widthInPix, heightInPix) | |
| return itemDecoration | |
| } | |
| } | |
| private fun setDividerSize(left: Int, top: Int, right: Int, bottom: Int) { | |
| mDivider.setBounds(left, top, right, bottom) | |
| } | |
| fun setWidth(widthInPix: Int) { | |
| setDividerSize(mDivider.bounds.left | |
| , mDivider.bounds.top | |
| , widthInPix | |
| , mDivider.bounds.bottom) | |
| } | |
| fun setHeight(heightInPix: Int) { | |
| setDividerSize(mDivider.bounds.left | |
| , mDivider.bounds.top | |
| , mDivider.bounds.right | |
| , heightInPix) | |
| } | |
| override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) { | |
| if (mOrientation == LinearLayoutManager.HORIZONTAL) { | |
| drawHorizontalDividers(canvas, parent) | |
| } else if (mOrientation == LinearLayoutManager.VERTICAL) { | |
| drawVerticalDividers(canvas, parent) | |
| } | |
| } | |
| override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { | |
| super.getItemOffsets(outRect, view, parent, state) | |
| if (parent.getChildAdapterPosition(view) == 0) { | |
| return | |
| } | |
| mOrientation = (parent.layoutManager as LinearLayoutManager?)!!.orientation | |
| if (mOrientation == LinearLayoutManager.HORIZONTAL) { | |
| if (ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_LTR) { | |
| outRect.left = mDivider.intrinsicWidth | |
| } else { | |
| outRect.right = mDivider.intrinsicWidth | |
| } | |
| } else if (mOrientation == LinearLayoutManager.VERTICAL) { | |
| outRect.top = mDivider.intrinsicHeight | |
| } | |
| } | |
| private fun drawHorizontalDividers(canvas: Canvas, parent: RecyclerView) { | |
| val parentTop = parent.paddingTop | |
| val parentBottom = parent.height - parent.paddingBottom | |
| val childCount = parent.childCount | |
| for (i in 0 until childCount - 1) { | |
| val child = parent.getChildAt(i) | |
| val params = child.layoutParams as RecyclerView.LayoutParams | |
| val parentLeft = child.right + params.rightMargin | |
| val parentRight = parentLeft + mDivider.intrinsicWidth | |
| mDivider.setBounds(parentLeft, parentTop, parentRight, parentBottom) | |
| mDivider.draw(canvas) | |
| } | |
| } | |
| private fun drawVerticalDividers(canvas: Canvas, parent: RecyclerView) { | |
| val parentLeft = parent.paddingLeft | |
| val parentRight = parent.width - parent.paddingRight | |
| val childCount = parent.childCount | |
| for (i in 0 until childCount - 1) { | |
| val child = parent.getChildAt(i) | |
| val params = child.layoutParams as RecyclerView.LayoutParams | |
| val parentTop = child.bottom + params.bottomMargin | |
| val parentBottom = parentTop + mDivider.intrinsicHeight | |
| mDivider.setBounds(parentLeft, parentTop, parentRight, parentBottom) | |
| mDivider.draw(canvas) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment