refactor(activity): 优化 MasterScaleActivity 布局常量与显示逻辑
This commit is contained in:
@@ -31,6 +31,20 @@ private fun android.content.Context.cellSize(columns: Int): Int {
|
|||||||
return usable / columns
|
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 角色显示)
|
* 主设备秤数据总览页面(仅 MASTER 角色显示)
|
||||||
* 聚合展示所有设备(本机 + 远端子设备)的实时秤数据
|
* 聚合展示所有设备(本机 + 远端子设备)的实时秤数据
|
||||||
@@ -83,7 +97,9 @@ class MasterScaleActivity : BaseActivity() {
|
|||||||
val groups = scaleMap.values
|
val groups = scaleMap.values
|
||||||
.groupBy { it.deviceId }
|
.groupBy { it.deviceId }
|
||||||
.entries
|
.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) ->
|
.map { (deviceId, scales) ->
|
||||||
DeviceGroup(
|
DeviceGroup(
|
||||||
deviceId = deviceId,
|
deviceId = deviceId,
|
||||||
@@ -136,9 +152,9 @@ class MasterScaleActivity : BaseActivity() {
|
|||||||
|
|
||||||
// 根据设备号选择不同的网格布局策略
|
// 根据设备号选择不同的网格布局策略
|
||||||
when (group.deviceId) {
|
when (group.deviceId) {
|
||||||
"a46fa55c-113c-3511-bb1f-41e5eff77c4b" -> buildLayout22(container, group.scales)
|
DEVICE_ID_22 -> buildLayout22(container, group.scales)
|
||||||
"1038da9f-c6eb-326e-a1d9-d6d3af978b22" -> buildLayout18(container, group.scales)
|
DEVICE_ID_18 -> buildLayout18(container, group.scales)
|
||||||
"8fc2ab34-2137-3112-acca-f884ea8736d4" -> buildLayout2(container, group.scales)
|
DEVICE_ID_2 -> buildLayout2(container, group.scales)
|
||||||
else -> buildLayoutDefault(container, group.scales)
|
else -> buildLayoutDefault(container, group.scales)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,9 +173,8 @@ class MasterScaleActivity : BaseActivity() {
|
|||||||
val smallCellSize = rightColW - margin * 2
|
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 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 {
|
val outerRow = LinearLayout(container.context).apply {
|
||||||
orientation = LinearLayout.HORIZONTAL
|
orientation = LinearLayout.HORIZONTAL
|
||||||
@@ -169,13 +184,21 @@ class MasterScaleActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
var index = 0
|
var index = 0
|
||||||
// 前2列:权重1.5,每列2个大格子(正方形)
|
// 前2列:权重1.5,每列2个大格子(正方形)
|
||||||
repeat(2) {
|
repeat(2) { colIdx ->
|
||||||
val col = LinearLayout(container.context).apply {
|
val col = LinearLayout(container.context).apply {
|
||||||
orientation = LinearLayout.VERTICAL
|
orientation = LinearLayout.VERTICAL
|
||||||
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.5f)
|
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.5f)
|
||||||
}
|
}
|
||||||
repeat(2) {
|
repeat(2) { rowIdx ->
|
||||||
col.addView(makeScaleCellFixed(sorted.getOrNull(index++), largeCellSize, largeCellSize, margin))
|
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)
|
outerRow.addView(col)
|
||||||
}
|
}
|
||||||
@@ -236,9 +259,8 @@ class MasterScaleActivity : BaseActivity() {
|
|||||||
private fun buildLayout18(container: LinearLayout, scales: List<ScaleData>) {
|
private fun buildLayout18(container: LinearLayout, scales: List<ScaleData>) {
|
||||||
val cellSize = container.context.cellSize(6)
|
val cellSize = container.context.cellSize(6)
|
||||||
// 按指定顺序重排(address 从1开始,转为0-based index取值)
|
// 按指定顺序重排(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 scaleByAddress = scales.associateBy { it.address }
|
||||||
val sorted = order.map { scaleByAddress[it] }
|
val sorted = SCALE_ORDER_18.map { scaleByAddress[it] }
|
||||||
|
|
||||||
var index = 0
|
var index = 0
|
||||||
// 第1行:增大 margin 使格子看起来更小
|
// 第1行:增大 margin 使格子看起来更小
|
||||||
@@ -272,10 +294,11 @@ class MasterScaleActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
val row = LinearLayout(container.context).apply {
|
val row = LinearLayout(container.context).apply {
|
||||||
orientation = LinearLayout.HORIZONTAL
|
orientation = LinearLayout.HORIZONTAL
|
||||||
|
gravity = Gravity.CENTER_VERTICAL
|
||||||
layoutParams = LinearLayout.LayoutParams(
|
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) }
|
).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)
|
setBackgroundResource(R.drawable.shape_scale_cell)
|
||||||
}
|
}
|
||||||
row.addView(makeTextView("秤 ${scale.address}", 22, weight = 1f))
|
row.addView(makeTextView("秤 ${scale.address}", 22, weight = 1f))
|
||||||
|
|||||||
@@ -56,7 +56,7 @@
|
|||||||
app:layout_constraintTop_toBottomOf="@id/tvConnectionStatus"
|
app:layout_constraintTop_toBottomOf="@id/tvConnectionStatus"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
android:overScrollMode="never"
|
android:overScrollMode="never"
|
||||||
android:padding="16dp"
|
android:padding="0dp"
|
||||||
tools:listitem="@layout/list_item_scale_data"/>
|
tools:listitem="@layout/list_item_scale_data"/>
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user