refactor(layout): 优化布局尺寸计算逻辑并提取公共工具类
- 添加 SizeTool 工具类统一管理 Flexbox 布局尺寸计算 - 在 MasterScaleActivity 中使用 SizeTool 替代硬编码计算 - 在 SeasoningConfigFragment 中使用 SizeTool 重构尺寸计算 - 在 SlaveActivity 中应用统一的尺寸计算方法 - 更新 Seasoning18GridAdapter 和 Seasoning22GridAdapter 使用现代化布局参数设置 - 调整 fragment_seasoning_config.xml 中的边距和内边距数值 - 为布局文件添加 tools:itemCount 属性支持预览功能
This commit is contained in:
@@ -33,6 +33,7 @@ import com.shuwei.dish.match.scale.ScaleServiceManager
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import com.shuwei.dish.match.utils.NetworkUtil
|
||||
import com.shuwei.dish.match.utils.SizeTool
|
||||
import com.shuwei.dish.match.utils.WeightUtil
|
||||
import com.shuwei.dish.match.utils.ext.dp
|
||||
import com.shuwei.dish.match.utils.ext.toast
|
||||
@@ -51,7 +52,7 @@ private const val VIEW_TYPE_SCALE1 = 3
|
||||
class MasterScaleActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: ActivityMasterScaleBinding
|
||||
|
||||
private val size by lazy { SizeTool.getFlexLayoutSize(10.dp) }
|
||||
private data class DeviceGroup(
|
||||
val deviceId: String,
|
||||
val ip: String,
|
||||
@@ -357,12 +358,7 @@ class MasterScaleActivity : BaseActivity() {
|
||||
b.tvDeviceLabel.text = "设备:${group.deviceId}"
|
||||
|
||||
if (innerAdapter == null) {
|
||||
val screenWidth = resources.displayMetrics.widthPixels
|
||||
val usable = screenWidth - 8 * 4.dp - 10.dp
|
||||
val smallSize = usable / 9
|
||||
val largeSize = (usable - smallSize * 6) / 2
|
||||
val rvHeight = smallSize * 3 + 3 * 4.dp
|
||||
b.rvScales.layoutParams = b.rvScales.layoutParams.also { it.height = rvHeight }
|
||||
b.rvScales.layoutParams = b.rvScales.layoutParams.also { it.height = size.rvHeight }
|
||||
b.rvScales.layoutManager = FlexboxLayoutManager(b.root.context).apply {
|
||||
flexDirection = FlexDirection.COLUMN
|
||||
flexWrap = FlexWrap.WRAP
|
||||
@@ -370,8 +366,8 @@ class MasterScaleActivity : BaseActivity() {
|
||||
}
|
||||
b.rvScales.itemAnimator = null
|
||||
innerAdapter = Seasoning22GridAdapter(
|
||||
largeSize,
|
||||
smallSize,
|
||||
size.largeSize,
|
||||
size.smallSize,
|
||||
showNameOnly = false,
|
||||
showAddress = true
|
||||
).apply {
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.shuwei.dish.match.scale.ScaleData
|
||||
import com.shuwei.dish.match.scale.ScaleDeviceConfig
|
||||
import com.shuwei.dish.match.scale.ScaleServiceManager
|
||||
import com.shuwei.dish.match.utils.NetworkUtil
|
||||
import com.shuwei.dish.match.utils.SizeTool
|
||||
import com.shuwei.dish.match.utils.WeightUtil
|
||||
import com.shuwei.dish.match.utils.ext.dp
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -61,6 +62,7 @@ class SlaveActivity : BaseActivity() {
|
||||
private val mainHandler = Handler(Looper.getMainLooper())
|
||||
/** 当前高亮清除任务,新高亮触发时取消旧任务 */
|
||||
private var clearHighlightRunnable: Runnable? = null
|
||||
private val size by lazy { SizeTool.getFlexLayoutSize(10.dp) }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -90,13 +92,8 @@ class SlaveActivity : BaseActivity() {
|
||||
binding.rvScaleList.itemAnimator = null
|
||||
when (GlobalData.deviceId) {
|
||||
ScaleDeviceConfig.DEVICE_ID_22 -> {
|
||||
val screenWidth = resources.displayMetrics.widthPixels
|
||||
val usable = screenWidth - 8 * 4.dp - 10.dp
|
||||
val smallSize = usable / 9
|
||||
val largeSize = (usable - smallSize * 6) / 2
|
||||
val rvHeight = smallSize * 3 + 3 * 4.dp
|
||||
binding.rvScaleList.updateLayoutParams<LinearLayout.LayoutParams> {
|
||||
height = rvHeight
|
||||
height = size.rvHeight
|
||||
weight = 0f
|
||||
topMargin = 20.dp
|
||||
}
|
||||
@@ -105,7 +102,7 @@ class SlaveActivity : BaseActivity() {
|
||||
flexWrap = FlexWrap.WRAP
|
||||
alignItems = AlignItems.FLEX_START
|
||||
}
|
||||
scale22Adapter = Seasoning22GridAdapter(largeSize, smallSize, showNameOnly = false).also {
|
||||
scale22Adapter = Seasoning22GridAdapter(size.largeSize, size.smallSize, showNameOnly = false).also {
|
||||
it.onItemClick = { scale, _ -> showTareDialog(scale.address) }
|
||||
}
|
||||
binding.rvScaleList.adapter = scale22Adapter
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.shuwei.dish.match.scale.ScaleEvent
|
||||
import com.shuwei.dish.match.scale.ScaleServiceManager
|
||||
import com.shuwei.dish.match.scale.ScaleEvent.SlotConfig
|
||||
import com.shuwei.dish.match.ui.SettingActivity
|
||||
import com.shuwei.dish.match.utils.SizeTool
|
||||
import com.shuwei.dish.match.utils.WeightUtil
|
||||
import com.shuwei.dish.match.utils.ext.dp
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -50,7 +51,7 @@ class SeasoningConfigFragment : BaseFragment<FragmentSeasoningConfigBinding>() {
|
||||
// private lateinit var appViewModel: AppViewModel
|
||||
|
||||
private val adapter22 by lazy { buildAdapter22() }
|
||||
private val adapter18 by lazy { Seasoning18GridAdapter(showNameOnly = true) }
|
||||
private val adapter18 by lazy { Seasoning18GridAdapter(showNameOnly = true, leftRightSpace = 20.dp) }
|
||||
|
||||
/** 各秤的上一次重量快照,key 格式 "{deviceId}#{address}",用于计算 delta */
|
||||
private val weightSnapshot = mutableMapOf<String, Double>()
|
||||
@@ -150,24 +151,23 @@ class SeasoningConfigFragment : BaseFragment<FragmentSeasoningConfigBinding>() {
|
||||
// appViewModel = ViewModelProvider(this, factory)[AppViewModel::class.java]
|
||||
// }
|
||||
|
||||
|
||||
private val size by lazy { SizeTool.getFlexLayoutSize(20.dp) }
|
||||
/** 构建上层(22格)Adapter,需要先计算格子尺寸 */
|
||||
private fun buildAdapter22(): Seasoning22GridAdapter {
|
||||
val usable = resources.displayMetrics.widthPixels - 8 * 4.dp - 10.dp
|
||||
val smallSize = usable / 9
|
||||
val largeSize = (usable - smallSize * 6) / 2
|
||||
return Seasoning22GridAdapter(largeSize, smallSize, showNameOnly = true)
|
||||
return Seasoning22GridAdapter(
|
||||
largeSize = size.largeSize,
|
||||
smallSize = size.smallSize,
|
||||
showNameOnly = true
|
||||
)
|
||||
}
|
||||
|
||||
/** 配置两个 RecyclerView */
|
||||
private fun setupRecyclerViews() {
|
||||
// 上层:FlexboxLayoutManager,仿 MasterScaleActivity 中 Scale22VH 的布局方式
|
||||
// rvTop 高度 = 小格子尺寸 * 3行 + 3 * 间距,与 Scale22VH 保持一致
|
||||
val screenWidth = resources.displayMetrics.widthPixels
|
||||
val usable = screenWidth - 8 * 4.dp - 10.dp
|
||||
val smallSize = usable / 9
|
||||
val rvTopHeight = smallSize * 3 + 3 * 4.dp
|
||||
binding.rvTop.apply {
|
||||
layoutParams = layoutParams.also { it.height = rvTopHeight }
|
||||
layoutParams = layoutParams.also { it.height = size.rvHeight }
|
||||
layoutManager = FlexboxLayoutManager(requireContext()).apply {
|
||||
flexDirection = FlexDirection.COLUMN
|
||||
flexWrap = FlexWrap.WRAP
|
||||
|
||||
Reference in New Issue
Block a user