任务页五栏联动筛选:项目/时间/重要度/紧急度/状态,流式换行布局

This commit is contained in:
XIGE
2026-11-28 18:17:27 +08:00
parent cc74753bb9
commit 5458695834
5 changed files with 322 additions and 113 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,93 @@
package com.focusbox.app.util
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
/**
* 流式布局:一行放不下就自动换行,替代只能左右滑的 HorizontalScrollView。
* 筛选胶囊数量不定(项目可能很多),换行比滑动更容易一眼看清全部选项。
* 子 View 需要 MarginLayoutParams(makeChip 默认给了 marginEnd)。
*/
class FlowLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : ViewGroup(context, attrs) {
private val hGap = dpi(8f)
private val vGap = dpi(6f)
override fun generateDefaultLayoutParams(): LayoutParams =
MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams =
MarginLayoutParams(context, attrs)
override fun generateLayoutParams(p: LayoutParams?): LayoutParams = MarginLayoutParams(p)
override fun checkLayoutParams(p: LayoutParams?): Boolean = p is MarginLayoutParams
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val maxW = MeasureSpec.getSize(widthMeasureSpec) - paddingLeft - paddingRight
var lineW = 0
var totalH = paddingTop + paddingBottom
var lineH = 0
for (i in 0 until childCount) {
val child = getChildAt(i)
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)
val lp = child.layoutParams as MarginLayoutParams
val w = child.measuredWidth + lp.leftMargin + lp.rightMargin
val h = child.measuredHeight + lp.topMargin + lp.bottomMargin
if (lineW > 0 && lineW + hGap + w > maxW) {
// 换行:累计上一行高度
totalH += lineH + vGap
lineW = w
lineH = h
} else {
if (lineW > 0) lineW += hGap
lineW += w
lineH = maxOf(lineH, h)
}
}
totalH += lineH
setMeasuredDimension(
MeasureSpec.getSize(widthMeasureSpec),
resolveSize(
maxOf(totalH, suggestedMinimumHeight),
heightMeasureSpec,
),
)
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
val maxW = r - l - paddingLeft - paddingRight
var x = paddingLeft
var y = paddingTop
var lineH = 0
for (i in 0 until childCount) {
val child = getChildAt(i)
val lp = child.layoutParams as MarginLayoutParams
val w = child.measuredWidth + lp.leftMargin + lp.rightMargin
val h = child.measuredHeight + lp.topMargin + lp.bottomMargin
if (x - paddingLeft > 0 && x + w > maxW + paddingLeft) {
x = paddingLeft
y += lineH + vGap
lineH = 0
}
// 行内垂直居中,胶囊高矮不齐时对齐中轴线
val childTop = y + (lineH - h).coerceAtLeast(0) / 2
child.layout(
x + lp.leftMargin,
childTop + lp.topMargin,
x + lp.leftMargin + child.measuredWidth,
childTop + lp.topMargin + child.measuredHeight,
)
x += w + hGap
lineH = maxOf(lineH, h)
}
}
}