From 69dc77f06ecb3801fd83ddcc8585374a5f848c53 Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Tue, 14 Apr 2026 16:12:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(activity):=20=E4=BC=98=E5=8C=96=20Mast?= =?UTF-8?q?erScaleActivity=20=E5=B8=83=E5=B1=80=E5=B8=B8=E9=87=8F=E4=B8=8E?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dish/match/ui/MasterScaleActivity.kt | 51 ++++++++++++++----- .../main/res/layout/activity_master_scale.xml | 2 +- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt index 25c298d..1b78046 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt @@ -31,6 +31,20 @@ private fun android.content.Context.cellSize(columns: Int): Int { return usable / columns } +private const val DEVICE_ID_2 = "8fc2ab34-2137-3112-acca-f884ea8736d4" // 2个秤 +private const val DEVICE_ID_22 = "a46fa55c-113c-3511-bb1f-41e5eff77c4b" // 22个秤 +private const val DEVICE_ID_18 = "1038da9f-c6eb-326e-a1d9-d6d3af978b22" // 18个秤 +private const val DEVICE_ID_1 = "7cc0f6ea-f13d-3013-a867-fc998eb554ac" // 1个秤 + +/** 22个秤的显示顺序 */ +private val SCALE_ORDER_22 = listOf(20,22,19,21,6,12,18,5,11,17,4,10,16,3,9,15,2,8,14,1,7,13) + +/** 18个秤的显示顺序 */ +private val SCALE_ORDER_18 = listOf(6,5,4,3,2,1, 12,11,10,9,8,7, 18,17,16,15,14,13) + +/** 设备显示顺序,第一个为本机设备号,其余按指定顺序排列,不在列表中的设备追加到末尾 */ +private val deviceOrder = listOf(DEVICE_ID_2, DEVICE_ID_22, DEVICE_ID_18, DEVICE_ID_1) + /** * 主设备秤数据总览页面(仅 MASTER 角色显示) * 聚合展示所有设备(本机 + 远端子设备)的实时秤数据 @@ -83,7 +97,9 @@ class MasterScaleActivity : BaseActivity() { val groups = scaleMap.values .groupBy { it.deviceId } .entries - .sortedWith(compareBy({ if (it.key == localId) 0 else 1 }, { it.key })) + .sortedWith(compareBy { idx -> + deviceOrder.indexOf(idx.key).let { if (it == -1) Int.MAX_VALUE else it } + }) .map { (deviceId, scales) -> DeviceGroup( deviceId = deviceId, @@ -136,10 +152,10 @@ class MasterScaleActivity : BaseActivity() { // 根据设备号选择不同的网格布局策略 when (group.deviceId) { - "a46fa55c-113c-3511-bb1f-41e5eff77c4b" -> buildLayout22(container, group.scales) - "1038da9f-c6eb-326e-a1d9-d6d3af978b22" -> buildLayout18(container, group.scales) - "8fc2ab34-2137-3112-acca-f884ea8736d4" -> buildLayout2(container, group.scales) - else -> buildLayoutDefault(container, group.scales) + DEVICE_ID_22 -> buildLayout22(container, group.scales) + DEVICE_ID_18 -> buildLayout18(container, group.scales) + DEVICE_ID_2 -> buildLayout2(container, group.scales) + else -> buildLayoutDefault(container, group.scales) } } } @@ -157,9 +173,8 @@ class MasterScaleActivity : BaseActivity() { val smallCellSize = rightColW - margin * 2 // 按指定顺序重排 - val order = listOf(20,22,19,21,6,12,18,5,11,17,4,10,16,3,9,15,2,8,14,1,7,13) val scaleByAddress = scales.associateBy { it.address } - val sorted = order.map { scaleByAddress[it] } + val sorted = SCALE_ORDER_22.map { scaleByAddress[it] } val outerRow = LinearLayout(container.context).apply { orientation = LinearLayout.HORIZONTAL @@ -169,13 +184,21 @@ class MasterScaleActivity : BaseActivity() { } var index = 0 // 前2列:权重1.5,每列2个大格子(正方形) - repeat(2) { + repeat(2) { colIdx -> val col = LinearLayout(container.context).apply { orientation = LinearLayout.VERTICAL layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.5f) } - repeat(2) { - col.addView(makeScaleCellFixed(sorted.getOrNull(index++), largeCellSize, largeCellSize, margin)) + repeat(2) { rowIdx -> + val cell = makeScaleCellFixed(sorted.getOrNull(index++), largeCellSize, largeCellSize, margin) + // 两列中间水平间距翻倍(第1列右边距、第2列左边距),列内两格子中间垂直间距翻倍,外侧保持不变 + (cell.layoutParams as? LinearLayout.LayoutParams)?.apply { + bottomMargin = if (rowIdx == 0) margin * 2 else margin + topMargin = if (rowIdx == 1) margin * 2 else margin + rightMargin = if (colIdx == 0) margin * 2 else margin + leftMargin = if (colIdx == 1) margin * 2 else margin + } + col.addView(cell) } outerRow.addView(col) } @@ -236,9 +259,8 @@ class MasterScaleActivity : BaseActivity() { private fun buildLayout18(container: LinearLayout, scales: List) { val cellSize = container.context.cellSize(6) // 按指定顺序重排(address 从1开始,转为0-based index取值) - val order = listOf(6,5,4,3,2,1, 12,11,10,9,8,7, 18,17,16,15,14,13) val scaleByAddress = scales.associateBy { it.address } - val sorted = order.map { scaleByAddress[it] } + val sorted = SCALE_ORDER_18.map { scaleByAddress[it] } var index = 0 // 第1行:增大 margin 使格子看起来更小 @@ -272,10 +294,11 @@ class MasterScaleActivity : BaseActivity() { } val row = LinearLayout(container.context).apply { orientation = LinearLayout.HORIZONTAL + gravity = Gravity.CENTER_VERTICAL layoutParams = LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT + LinearLayout.LayoutParams.MATCH_PARENT, 120.dp ).also { it.setMargins(2.dp, 4.dp, 2.dp, 4.dp) } - setPadding(12.dp, 16.dp, 12.dp, 16.dp) + setPadding(30.dp, 0, 0, 0) setBackgroundResource(R.drawable.shape_scale_cell) } row.addView(makeTextView("秤 ${scale.address}", 22, weight = 1f)) diff --git a/app/src/main/res/layout/activity_master_scale.xml b/app/src/main/res/layout/activity_master_scale.xml index f35b3e7..770efd6 100644 --- a/app/src/main/res/layout/activity_master_scale.xml +++ b/app/src/main/res/layout/activity_master_scale.xml @@ -56,7 +56,7 @@ app:layout_constraintTop_toBottomOf="@id/tvConnectionStatus" app:layout_constraintBottom_toBottomOf="parent" android:overScrollMode="never" - android:padding="16dp" + android:padding="0dp" tools:listitem="@layout/list_item_scale_data"/>