优化调料格子
This commit is contained in:
@@ -1,14 +1,24 @@
|
||||
package com.shuwei.dish.match.adapter
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Typeface
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.style.AbsoluteSizeSpan
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import android.text.style.LineHeightSpan
|
||||
import android.text.style.StyleSpan
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.graphics.toColorInt
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import com.chad.library.adapter4.BaseQuickAdapter
|
||||
import com.chad.library.adapter4.viewholder.QuickViewHolder
|
||||
import com.shuwei.dish.match.databinding.ListItemCellBinding
|
||||
import com.shuwei.dish.match.model.GoodsItem
|
||||
import com.shuwei.dish.match.utils.ext.appendText
|
||||
import com.shuwei.dish.match.utils.ext.buildSpannableString
|
||||
import com.shuwei.dish.match.utils.ext.dp
|
||||
import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace
|
||||
|
||||
/**
|
||||
* 调料格子列表适配器,使用 list_item_cell 布局展示调料名称与用量
|
||||
@@ -24,8 +34,8 @@ class SeasoningAdapter(list: MutableList<GoodsItem>) :
|
||||
|
||||
override fun onBindViewHolder(holder: VH, position: Int, item: GoodsItem?) {
|
||||
item ?: return
|
||||
// 第4个格子(index=3)使用大尺寸,其余使用标准尺寸
|
||||
val isLarge = position == 3
|
||||
// 第10个格子(index=9)使用大尺寸,其余使用标准尺寸
|
||||
val isLarge = position == 9
|
||||
holder.binding.root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||
width = if (isLarge) 467.dp else 226.dp
|
||||
height = if (isLarge) 255.dp else 120.dp
|
||||
@@ -34,8 +44,36 @@ class SeasoningAdapter(list: MutableList<GoodsItem>) :
|
||||
topMargin = 7.dp
|
||||
bottomMargin = 8.dp
|
||||
}
|
||||
holder.binding.tvCell.text = TextCellAdapter.getTextSpan(item)
|
||||
holder.binding.tvCell.text = getTextSpan(position, item)
|
||||
}
|
||||
|
||||
inner class VH(val binding: ListItemCellBinding) : QuickViewHolder(binding.root)
|
||||
|
||||
fun getTextSpan(position: Int, item: GoodsItem): SpannableStringBuilder {
|
||||
val endIndex = if (position == 9) 10 else 6
|
||||
var name = if (item.goodsName.isNullOrBlank()) "未配置" else item.goodsName!!
|
||||
if (name.length > endIndex) {
|
||||
name = "${name.substring(0, endIndex)}…"
|
||||
}
|
||||
return buildSpannableString {
|
||||
appendText(
|
||||
name,
|
||||
ForegroundColorSpan(if (item.goodsName.isNullOrBlank()) "#999999".toColorInt() else "#000000".toColorInt()),
|
||||
StyleSpan(Typeface.BOLD),
|
||||
AbsoluteSizeSpan(28, true),
|
||||
LineHeightSpan { text, start, end, spanstartv, v, fm ->
|
||||
fm.descent += 10.dp // 增加行间距
|
||||
}
|
||||
)
|
||||
append("\n")
|
||||
val weight = item.useWeight ?: 0.toDouble()
|
||||
val weightColor = if (weight == 0.toDouble()) "#999999" else "#00BC71"
|
||||
appendText(
|
||||
// "${DecimalFormat("#").format(weight)}g",
|
||||
"${weight.roundedOneDecimalPlace()}g",
|
||||
ForegroundColorSpan(weightColor.toColorInt()),
|
||||
AbsoluteSizeSpan(30, true)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,9 +105,6 @@ class TextCellAdapter(var list: MutableList<SeasoningEntity>) :
|
||||
// }
|
||||
// }
|
||||
|
||||
fun getTextSpan(item: GoodsItem): SpannableStringBuilder {
|
||||
return getTextSpan(item.toSeasoningEntity())
|
||||
}
|
||||
fun getTextSpan(item: SeasoningEntity): SpannableStringBuilder {
|
||||
val endIndex = if (item.sort == 9) 10 else 6
|
||||
var name = if (item.goodsName.isNullOrBlank()) "未配置" else item.goodsName!!
|
||||
|
||||
@@ -4,6 +4,7 @@ import androidx.room.Entity
|
||||
import androidx.room.Ignore
|
||||
import androidx.room.PrimaryKey
|
||||
import com.shuwei.dish.match.db.BaseEntity
|
||||
import com.shuwei.dish.match.model.GoodsItem
|
||||
import com.shuwei.dish.match.utils.DateTimeUtil
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@@ -78,4 +79,19 @@ var isDel: Int = 0,
|
||||
@Ignore var isClicked: Boolean = false
|
||||
|
||||
constructor() : this(goodsId = "") // 必需的空构造
|
||||
|
||||
/** 转换为 GoodsItem,用于 UI 层展示 */
|
||||
fun toGoodsItem(): GoodsItem = GoodsItem(
|
||||
goodsId = goodsId,
|
||||
goodsName = goodsName,
|
||||
popularName = popularName,
|
||||
zjmCode = zjmCode,
|
||||
materId = materId,
|
||||
materialType = materialType,
|
||||
useWeight = useWeight,
|
||||
relateionType = relateionType,
|
||||
allEdible = allEdible,
|
||||
goodsOrRelationCode = goodsOrRelationCode,
|
||||
rawMaterialsType = rawMaterialsType
|
||||
)
|
||||
}
|
||||
|
||||
@@ -9,20 +9,16 @@ import android.text.style.LineHeightSpan
|
||||
import android.text.style.StyleSpan
|
||||
import android.util.Log
|
||||
import android.util.SparseArray
|
||||
import android.util.SparseIntArray
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.graphics.toColorInt
|
||||
import androidx.core.view.forEach
|
||||
import com.google.android.flexbox.FlexDirection
|
||||
import com.google.android.flexbox.FlexboxLayoutManager
|
||||
import com.google.android.flexbox.FlexWrap
|
||||
import com.shuwei.dish.match.base.BaseActivity
|
||||
import com.shuwei.dish.match.databinding.LayoutFoodRemindBinding
|
||||
|
||||
import com.shuwei.dish.match.dialog.CommonDialog
|
||||
import com.shuwei.dish.match.db.entity.SeasoningEntity
|
||||
import com.shuwei.dish.match.db.entity.ResetReasoningRecord
|
||||
import com.shuwei.dish.match.model.GoodsItem
|
||||
import com.shuwei.dish.match.utils.AddressUtil
|
||||
import com.shuwei.dish.match.utils.JsonAssetsLoader
|
||||
import com.shuwei.dish.match.utils.MultiClickDetector
|
||||
import com.shuwei.dish.match.utils.WeightUtil
|
||||
import com.shuwei.dish.match.utils.ext.appendText
|
||||
@@ -31,12 +27,10 @@ import com.shuwei.dish.match.utils.ext.dp
|
||||
import com.shuwei.dish.match.utils.ext.startActivity
|
||||
import com.shuwei.dish.match.utils.ext.toast
|
||||
import com.shuwei.dish.match.utils.ext.visible
|
||||
import com.shuwei.dish.match.R
|
||||
import com.shuwei.dish.match.adapter.TextCellAdapter
|
||||
import com.shuwei.dish.match.adapter.SeasoningAdapter
|
||||
import com.shuwei.dish.match.databinding.ActivityDeviceConfigBinding
|
||||
import com.shuwei.dish.match.dialog.BottomDialog2
|
||||
import com.shuwei.dish.match.utils.SpTool
|
||||
import com.shuwei.dish.match.utils.ext.clickWithDebounce
|
||||
import com.shuwei.dish.match.utils.ext.gone
|
||||
|
||||
class DeviceConfigActivity : BaseActivity() {
|
||||
@@ -123,7 +117,7 @@ class DeviceConfigActivity : BaseActivity() {
|
||||
finish()
|
||||
}
|
||||
addWeighListener()
|
||||
addGridItemListener()
|
||||
setupRecyclerView()
|
||||
loadSeasoning()
|
||||
}
|
||||
|
||||
@@ -133,26 +127,34 @@ class DeviceConfigActivity : BaseActivity() {
|
||||
getWeight = { address, state, weight ->
|
||||
Log.d(TAG, "addWeighListener: address=$address,stat=$state,weight=$weight")
|
||||
weightArray.put(address, weight)
|
||||
val item = seasoningItems.firstOrNull { address == addressArray[it.sort] }
|
||||
item?.let {
|
||||
it.useWeight = weight.toDouble()
|
||||
// updateGridData(it)
|
||||
}
|
||||
// val item = seasoningItems.firstOrNull { address == addressArray[it.sort] }
|
||||
// item?.let {
|
||||
// it.useWeight = weight.toDouble()
|
||||
//// updateGridData(it)
|
||||
// }
|
||||
Log.d(TAG, "addWeighListener: update-----------------------")
|
||||
})
|
||||
}
|
||||
|
||||
private var seasoningItems = mutableListOf<SeasoningEntity>()
|
||||
private fun addGridItemListener() {
|
||||
binding.include.root.forEach { child ->
|
||||
child.clickWithDebounce {
|
||||
val sort = child.tag.toString().toInt()
|
||||
val entity = seasoningItems.firstOrNull { it.sort == sort } ?: SeasoningEntity()
|
||||
clickGridItem(sort, entity)
|
||||
private val seasoningItems = MutableList(12) { GoodsItem() }
|
||||
private val seasoningAdapter by lazy {
|
||||
SeasoningAdapter(seasoningItems).apply {
|
||||
setOnItemClickListener { adapter, _, position ->
|
||||
val item = adapter.getItem(position) ?: return@setOnItemClickListener
|
||||
clickGridItem(position,item.toSeasoningEntity())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupRecyclerView() {
|
||||
// 初始化 FlexboxLayoutManager,横向排列(FlexDirection.ROW)
|
||||
val flexboxLayoutManager = FlexboxLayoutManager(this).apply {
|
||||
flexDirection = FlexDirection.ROW
|
||||
flexWrap = FlexWrap.WRAP
|
||||
}
|
||||
binding.rvSeasoning.layoutManager = flexboxLayoutManager
|
||||
binding.rvSeasoning.adapter = seasoningAdapter
|
||||
}
|
||||
|
||||
val detector = MultiClickDetector(targetCount = 10, intervalMs = 800)
|
||||
|
||||
private fun defaultDataSettingDialog() {
|
||||
@@ -173,17 +175,16 @@ class DeviceConfigActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun loadSeasoning() {
|
||||
appViewModel.loadSeasoning {
|
||||
appViewModel.loadSeasoning { list ->
|
||||
seasoningItems.clear()
|
||||
seasoningItems.addAll(it)
|
||||
// initConfigData()
|
||||
// setGridData(seasoningItems)
|
||||
// 将 SeasoningEntity 转为 GoodsItem 后提交给 adapter
|
||||
val goodsItems = list.map { it.toGoodsItem() }
|
||||
seasoningAdapter.submitList(goodsItems)
|
||||
}
|
||||
}
|
||||
|
||||
private fun clickGridItem(sort: Int, entity: SeasoningEntity) {
|
||||
val currentAddress = AddressUtil.getWeighAddressArray().get(sort)
|
||||
// val seasoningEntity = list.first{it.sort == sort}
|
||||
BottomDialog2(
|
||||
weighIndex = sort,
|
||||
weighAddress = currentAddress,
|
||||
@@ -194,10 +195,21 @@ class DeviceConfigActivity : BaseActivity() {
|
||||
entity.materialType = 3
|
||||
entity.sort = sort
|
||||
saveSeasoning(entity) {
|
||||
val tag = sort.toString()
|
||||
val gridLayout = binding.include.root
|
||||
val frameLayout = gridLayout.findViewWithTag<FrameLayout>(tag)
|
||||
TextCellAdapter.loadLayout(frameLayout, entity)
|
||||
// 更新 adapter 中对应位置的数据
|
||||
val goodsItem = GoodsItem(
|
||||
goodsId = entity.goodsId,
|
||||
goodsName = entity.goodsName,
|
||||
popularName = entity.popularName,
|
||||
zjmCode = entity.zjmCode,
|
||||
materId = entity.materId,
|
||||
materialType = entity.materialType,
|
||||
useWeight = entity.useWeight,
|
||||
relateionType = entity.relateionType,
|
||||
allEdible = entity.allEdible,
|
||||
goodsOrRelationCode = entity.goodsOrRelationCode,
|
||||
rawMaterialsType = entity.rawMaterialsType
|
||||
)
|
||||
seasoningAdapter.set(sort, goodsItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,11 +128,10 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<include
|
||||
android:id="@+id/include"
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/rvSeasoning"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
layout="@layout/layout_grid"/>
|
||||
android:layout_height="725dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user