任务页五栏联动筛选:项目/时间/重要度/紧急度/状态,流式换行布局
This commit is contained in:
File diff suppressed because it is too large
Load Diff
93
src/main/java/com/focusbox/app/util/FlowLayout.kt
Normal file
93
src/main/java/com/focusbox/app/util/FlowLayout.kt
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,20 +45,21 @@
|
||||
android:contentDescription="@string/add_task" />
|
||||
</LinearLayout>
|
||||
|
||||
<HorizontalScrollView
|
||||
<!-- 五栏联动筛选:项目 / 时间 / 重要度 / 紧急度 / 状态,行内放不下自动换行 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/filterPanel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingStart="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:clipToPadding="false"
|
||||
android:scrollbars="none">
|
||||
android:paddingTop="4dp"
|
||||
android:paddingBottom="10dp" />
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/chipRow"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal" />
|
||||
</HorizontalScrollView>
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="@color/divider" />
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -62,6 +62,7 @@
|
||||
|
||||
<string name="tasks_title">任务</string>
|
||||
<string name="task_count">%1$d 个待办</string>
|
||||
<string name="task_count_all">%1$d 个任务</string>
|
||||
<string name="task_count_today">%1$d 个今日任务</string>
|
||||
<string name="task_count_today_mixed">%1$d 个待办 · 今日完成 %2$d 项</string>
|
||||
<string name="task_count_done">%1$d 个已完成</string>
|
||||
@@ -69,6 +70,19 @@
|
||||
<string name="filter_all">全部</string>
|
||||
<string name="filter_today">今日</string>
|
||||
<string name="filter_done">已完成</string>
|
||||
<string name="filter_project">项目</string>
|
||||
<string name="filter_time">时间</string>
|
||||
<string name="filter_importance">重要度</string>
|
||||
<string name="filter_urgency">紧急度</string>
|
||||
<string name="filter_status">状态</string>
|
||||
<string name="time_tomorrow">明天</string>
|
||||
<string name="time_this_week">本周</string>
|
||||
<string name="time_this_month">本月</string>
|
||||
<string name="time_anytime">随时</string>
|
||||
<string name="status_pending">未完成</string>
|
||||
<string name="urgency_high">紧急</string>
|
||||
<string name="urgency_mid">中</string>
|
||||
<string name="urgency_low">不急</string>
|
||||
<string name="show_done">显示已完成</string>
|
||||
<string name="hide_done">隐藏已完成</string>
|
||||
<string name="pending">待完成</string>
|
||||
|
||||
Reference in New Issue
Block a user