Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzzzer committed Feb 24, 2024
2 parents 06459ae + 30a5826 commit 19a1119
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,32 @@ class TagItemDecoration private constructor(private var builder: ItemBuilder) :
}

override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
if (!builder.fixable) {
return
}

val position = (parent.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
val currentView = parent.findViewHolderForLayoutPosition(position)?.itemView
val tag = builder.mData[position].getTitleTag()

currentView?.let {
var flag = false
//防止数组越界
if ((position + 1) < builder.mData.size) {
//当前第一个可见的Item的tag,不等于其后一个item的tag,说明悬浮的View要切换了
if (tag != builder.mData[position + 1].getTitleTag()) {
//当第一个可见的item在屏幕中还剩的高度小于title区域的高度时,我们也该开始做悬浮Title的“交换动画”
if ((it.height + it.top) < builder.itemTagHeight) {
c.save()
flag = true
c.translate(
0f,
(currentView.height + currentView.top - builder.itemTagHeight).toFloat()
)
}
}
}

val startX = currentView.left.toFloat()
val startY = 0f
val endX = currentView.right.toFloat()
Expand All @@ -94,6 +115,7 @@ class TagItemDecoration private constructor(private var builder: ItemBuilder) :
val tagY =
(builder.itemTagHeight - (builder.itemTagHeight / 2 - mBounds.height() / 2)).toFloat()
c.drawText(tag, tagX, tagY, mTagTextPaint)
if (flag) c.restore()
}
}

Expand Down Expand Up @@ -169,6 +191,9 @@ class TagItemDecoration private constructor(private var builder: ItemBuilder) :
//Tag的文本字体大小
internal var tagTextSize: Int = 18

//标签是否可固定在界面的顶部,默认为true,
internal var fixable: Boolean = true

/**
* 设置Item Tag的高度
*/
Expand Down Expand Up @@ -199,6 +224,12 @@ class TagItemDecoration private constructor(private var builder: ItemBuilder) :
*/
fun setTagTextColor(color: Int) = apply { tagTextColor = color }

/**
* 设置Tag是否固定在界面的顶部
* 如果设置为false,那么标签会随着滑动滑出界面外
*/
fun setTagFixable(fix: Boolean) = apply { fixable = fix }

/**
* 设置tag的文本字体大小
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RecyclerViewPage : BaseVBActivity<ActivityRecyclerViewPageBinding>() {
setItemTagHeight(UIUtils.dp2px(baseContext,25f))
setItemMargin(UIUtils.dp2px(baseContext,15f))
setItemBgColor(Color.parseColor("#FF0000"))
setTagFixable(true)
}.builder()
}

Expand Down

0 comments on commit 19a1119

Please sign in to comment.