Skip to content

Commit

Permalink
feat: 分割线函数divider新增参数stretch禁止拉伸
Browse files Browse the repository at this point in the history
  • Loading branch information
RebornQ authored and liangjingkanji committed Jul 16, 2024
1 parent 38bd597 commit 8f691b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
31 changes: 25 additions & 6 deletions brv/src/main/java/com/drake/brv/DefaultDecoration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class DefaultDecoration constructor(private val context: Context) : RecyclerView
*/
var orientation = DividerOrientation.HORIZONTAL

/** 是否拉伸图片, 暂不支持网格分割线 */
var stretch: Boolean = true

/**
* 列表是否被反转
*/
Expand Down Expand Up @@ -156,18 +159,24 @@ class DefaultDecoration constructor(private val context: Context) : RecyclerView

/**
* 将图片作为分割线, 图片宽高即分割线宽高
*
* @param stretch 是否拉伸图片, 默认为 true
*/
fun setDrawable(drawable: Drawable) {
fun setDrawable(drawable: Drawable, stretch: Boolean = true) {
divider = drawable
this.stretch = stretch
}

/**
* 将图片作为分割线, 图片宽高即分割线宽高
*
* @param stretch 是否拉伸图片, 默认为 true
*/
fun setDrawable(@DrawableRes drawableRes: Int) {
fun setDrawable(@DrawableRes drawableRes: Int, stretch: Boolean = true) {
val drawable = ContextCompat.getDrawable(context, drawableRes)
?: throw IllegalArgumentException("Drawable cannot be find")
divider = drawable
this.stretch = stretch
}
//</editor-fold>

Expand Down Expand Up @@ -453,8 +462,8 @@ class DefaultDecoration constructor(private val context: Context) : RecyclerView
*/
private fun drawHorizontal(canvas: Canvas, parent: RecyclerView, reverseLayout: Boolean) {
canvas.save()
val left: Int
val right: Int
var left: Int
var right: Int

if (parent.clipToPadding) {
left = parent.paddingLeft + this.marginStart
Expand Down Expand Up @@ -506,6 +515,11 @@ class DefaultDecoration constructor(private val context: Context) : RecyclerView
bottom = decoratedBounds.bottom
top = if (intrinsicHeight == -1) bottom - size else bottom - intrinsicHeight
}
if (intrinsicWidth != -1 && stretch.not()) {
val centerHorizontal = (left + right) / 2
left = centerHorizontal - intrinsicWidth / 2
right = centerHorizontal + intrinsicWidth / 2
}

if (startVisible && if (reverseLayout) edge.bottom else edge.top) {
setBounds(left, firstTop, right, firstBottom)
Expand All @@ -524,8 +538,8 @@ class DefaultDecoration constructor(private val context: Context) : RecyclerView
*/
private fun drawVertical(canvas: Canvas, parent: RecyclerView, reverseLayout: Boolean) {
canvas.save()
val top: Int
val bottom: Int
var top: Int
var bottom: Int

if (parent.clipToPadding) {
top = parent.paddingTop + marginStart
Expand Down Expand Up @@ -566,6 +580,11 @@ class DefaultDecoration constructor(private val context: Context) : RecyclerView

val right = (decoratedBounds.right + child.translationX).roundToInt()
val left = if (intrinsicWidth == -1) right - size else right - intrinsicWidth
if (intrinsicHeight != -1 && stretch.not()) {
val centerVertical = (top + bottom) / 2
top = centerVertical - intrinsicHeight / 2
bottom = centerVertical + intrinsicHeight / 2
}

if (startVisible && edge.left) {
setBounds(firstLeft, top, firstRight, bottom)
Expand Down
6 changes: 4 additions & 2 deletions brv/src/main/java/com/drake/brv/utils/RecyclerUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ fun RecyclerView.divider(
* 指定Drawable资源为分割线, 分割线的间距和宽度应在资源文件中配置
* @param drawable 描述分割线的drawable
* @param orientation 分割线方向, 仅[androidx.recyclerview.widget.GridLayoutManager]需要使用此参数, 其他LayoutManager都是根据其方向自动推断
* @param stretch 是否拉伸图片, 默认为 true, 暂不支持网格分割线
*/
fun RecyclerView.divider(
@DrawableRes drawable: Int,
orientation: DividerOrientation = DividerOrientation.HORIZONTAL
orientation: DividerOrientation = DividerOrientation.HORIZONTAL,
stretch: Boolean = true
): RecyclerView {
return divider {
setDrawable(drawable)
setDrawable(drawable, stretch)
this.orientation = orientation
}
}
Expand Down

0 comments on commit 8f691b8

Please sign in to comment.