diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt index 5da5beb..7f2256c 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt @@ -6,6 +6,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.content.ContextCompat +import androidx.core.view.updateLayoutParams import androidx.recyclerview.widget.RecyclerView import com.chad.library.adapter4.BaseQuickAdapter import com.chad.library.adapter4.viewholder.QuickViewHolder @@ -25,7 +26,8 @@ import com.shuwei.dish.match.utils.ext.dp */ class Seasoning18GridAdapter( private val showNameOnly: Boolean = false, - private val showAddress: Boolean = false + private val showAddress: Boolean = false, + private val leftRightSpace: Int = 10.dp ) : BaseQuickAdapter(mutableListOf()) { /** 空位哨兵,address=0 表示无效 */ @@ -47,12 +49,12 @@ class Seasoning18GridAdapter( val b = ListItemScale22RowBinding.inflate(LayoutInflater.from(context), parent, false) val margin = if (viewType == 0) 12.dp else 2.dp val screenWidth = parent.resources.displayMetrics.widthPixels - val cellSize = (screenWidth - 6 * margin * 2 - 10.dp) / 6 - (b.root.layoutParams as? ViewGroup.MarginLayoutParams)?.also { - it.width = cellSize - it.height = cellSize - if (viewType == 0) it.setMargins(margin, 0, margin, 2.dp) - else it.setMargins(margin, margin, margin, margin) + val cellSize = (screenWidth - 6 * margin * 2 - leftRightSpace) / 6 + b.root.updateLayoutParams { + width = cellSize + height = cellSize + if (viewType == 0) setMargins(margin, 0, margin, 2.dp) + else setMargins(margin, margin, margin, margin) } val baseSp = (cellSize / parent.resources.displayMetrics.density).toInt() b.tvAddress.textSize = (baseSp * 0.14f).coerceIn(8f, 12f) diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt index 9552b90..0ea05a6 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt @@ -6,6 +6,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.content.ContextCompat +import androidx.core.view.updateLayoutParams import androidx.recyclerview.widget.RecyclerView import com.chad.library.adapter4.BaseQuickAdapter import com.chad.library.adapter4.viewholder.QuickViewHolder @@ -51,7 +52,7 @@ class Seasoning22GridAdapter( override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH { val b = ListItemScale22RowBinding.inflate(LayoutInflater.from(context), parent, false) val cellSize = if (viewType == 1) largeSize else smallSize - (b.root.layoutParams as? FlexboxLayoutManager.LayoutParams)?.apply { + b.root.updateLayoutParams { width = cellSize height = cellSize setMargins(2.dp, 2.dp, 2.dp, 2.dp) 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 ff0e623..b027bbf 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 @@ -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 { diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt index 9b09a1a..83f2a8c 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/SlaveActivity.kt @@ -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 { - 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 diff --git a/app/src/main/java/com/shuwei/dish/match/ui/fragment/SeasoningConfigFragment.kt b/app/src/main/java/com/shuwei/dish/match/ui/fragment/SeasoningConfigFragment.kt index 5cb0c05..3fdb146 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/fragment/SeasoningConfigFragment.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/fragment/SeasoningConfigFragment.kt @@ -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() { // 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() @@ -150,24 +151,23 @@ class SeasoningConfigFragment : BaseFragment() { // 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 diff --git a/app/src/main/java/com/shuwei/dish/match/utils/SizeTool.kt b/app/src/main/java/com/shuwei/dish/match/utils/SizeTool.kt new file mode 100644 index 0000000..f54424f --- /dev/null +++ b/app/src/main/java/com/shuwei/dish/match/utils/SizeTool.kt @@ -0,0 +1,25 @@ +package com.shuwei.dish.match.utils + +import com.shuwei.dish.match.base.BaseApp +import com.shuwei.dish.match.utils.ext.dp + +data class FlexLayoutSize( + val smallSize: Int, + val largeSize: Int, + val rvHeight: Int +) + +object SizeTool { + + fun getFlexLayoutSize(leftRightSpace: Int = 10.dp): FlexLayoutSize { + // 上层:FlexboxLayoutManager,仿 MasterScaleActivity 中 Scale22VH 的布局方式 + // rvTop 高度 = 小格子尺寸 * 3行 + 3 * 间距,与 Scale22VH 保持一致 + val screenWidth = BaseApp.instance!!.resources.displayMetrics.widthPixels + val usable = screenWidth - 8 * 4.dp - leftRightSpace + val smallSize = usable / 9 + val largeSize = (usable - smallSize * 6) / 2 + val rvHeight = smallSize * 3 + 3 * 4.dp + return FlexLayoutSize(smallSize, largeSize, rvHeight) + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_master_scale.xml b/app/src/main/res/layout/activity_master_scale.xml index 403b48a..7e3babb 100644 --- a/app/src/main/res/layout/activity_master_scale.xml +++ b/app/src/main/res/layout/activity_master_scale.xml @@ -59,6 +59,7 @@ app:layout_constraintTop_toBottomOf="@id/tvConnectionStatus" app:layout_constraintBottom_toBottomOf="parent" android:overScrollMode="never" + tools:itemCount="4" tools:listitem="@layout/list_type_scale22"/> diff --git a/app/src/main/res/layout/fragment_seasoning_config.xml b/app/src/main/res/layout/fragment_seasoning_config.xml index ff7b4ce..981a9a9 100644 --- a/app/src/main/res/layout/fragment_seasoning_config.xml +++ b/app/src/main/res/layout/fragment_seasoning_config.xml @@ -8,9 +8,9 @@