diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/SeasoningAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/SeasoningAdapter.kt index a6471f3..4adfc85 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/SeasoningAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/SeasoningAdapter.kt @@ -1,79 +1,38 @@ -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 布局展示调料名称与用量 - * 第4个格子(position=3)为大格,其余为标准格 - */ -class SeasoningAdapter(list: MutableList) : - BaseQuickAdapter(list) { - - override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH { - val binding = ListItemCellBinding.inflate(LayoutInflater.from(context), parent, false) - return VH(binding) - } - - override fun onBindViewHolder(holder: VH, position: Int, item: GoodsItem?) { - item ?: return - // 第10个格子(index=9)使用大尺寸,其余使用标准尺寸 - val isLarge = position == 9 - holder.binding.root.updateLayoutParams { - width = if (isLarge) 467.dp else 226.dp - height = if (isLarge) 255.dp else 120.dp - leftMargin = 7.dp - rightMargin = 8.dp - topMargin = 7.dp - bottomMargin = 8.dp - } - 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) - ) - } - } -} +//package com.shuwei.dish.match.adapter +// +//import android.content.Context +//import android.view.LayoutInflater +//import android.view.ViewGroup +//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.dp +// +///** +// * 调料格子列表适配器,使用 list_item_cell 布局展示调料名称与用量 +// */ +//class SeasoningAdapter(list: MutableList) : +// BaseQuickAdapter(list) { +// +// override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH { +// val binding = ListItemCellBinding.inflate(LayoutInflater.from(context), parent, false) +// return VH(binding) +// } +// +// override fun onBindViewHolder(holder: VH, position: Int, item: GoodsItem?) { +// item ?: return +// holder.binding.root.updateLayoutParams { +// width = 226.dp +// height = 120.dp +// leftMargin = 7.dp +// rightMargin = 8.dp +// topMargin = 7.dp +// bottomMargin = 8.dp +// } +// holder.binding.tvCell.text = TextCellAdapter.getTextSpan(item.toSeasoningEntity()) +// } +// +// inner class VH(val binding: ListItemCellBinding) : QuickViewHolder(binding.root) +//} diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt index 9e0898d..54f1ab7 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/TextCellAdapter.kt @@ -1,137 +1,137 @@ -package com.shuwei.dish.match.adapter - -import android.graphics.Typeface -import android.icu.text.DecimalFormat -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 android.widget.FrameLayout -import android.widget.TextView -import androidx.core.content.ContextCompat -import androidx.core.graphics.toColorInt -import androidx.recyclerview.widget.RecyclerView -import com.chad.library.adapter4.viewholder.QuickViewHolder -import com.shuwei.dish.match.R -import com.shuwei.dish.match.databinding.ListItemCellBinding -import com.shuwei.dish.match.db.entity.SeasoningEntity -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.clickWithDebounce -import com.shuwei.dish.match.utils.ext.dp -import com.shuwei.dish.match.utils.ext.invisible -import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace -import com.shuwei.dish.match.utils.ext.visible - -class TextCellAdapter(var list: MutableList) : - RecyclerView.Adapter() { - - inner class VH(var binding: ListItemCellBinding) : QuickViewHolder(binding.root) - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { - val inflater = LayoutInflater.from(parent.context) - val binding = ListItemCellBinding.inflate(inflater, parent, false) - return VH(binding) - } - - override fun onBindViewHolder(holder: VH, position: Int) { - val item = list[position] - holder.binding.run { - if (position == 10 || position == 11 || position == 13 || position == 14) { - root.invisible() - } else { - root.visible() - } - loadCell(tvCell, item) - //root.setBackgroundResource(R.color.dish_green) - (root.layoutParams as RecyclerView.LayoutParams).let { -// it.bottomMargin = 15.dp - it.width = 226.dp - it.height = 120.dp -// it.rightMargin = 0 -// it.leftMargin = if (position % 3 == 0) 0 else 15.dp - - it.leftMargin = 7.dp - it.rightMargin = 8.dp - it.topMargin = 7.dp - it.bottomMargin = 8.dp - } - if (item.pageType == 1) { - root.clickWithDebounce { onCellClick?.invoke(position) } - } - } - } - - override fun getItemCount() = list.size - - var onCellClick: ((positon: Int) -> Unit)? = null - - companion object { - fun loadLayout(frameLayout: FrameLayout, item: SeasoningEntity) { - val child: TextView = frameLayout.getChildAt(0) as TextView - child.text = getTextSpan(item) - } - fun loadCell(tvCell: TextView, item: SeasoningEntity) { - tvCell.text = getTextSpan(item) - } -// fun loadCell2(tvCell: TextView, item: SeasoningEntity) { -// if (item.goodsName.isNullOrBlank()) { -// tvCell.run { -// text = buildSpannableString { -// appendText( -// "未配置", -// ForegroundColorSpan("#999999".toColorInt()), -// StyleSpan(Typeface.BOLD), -// AbsoluteSizeSpan(28, true), -// LineHeightSpan { text, start, end, spanstartv, v, fm -> -// fm.descent += 10.dp // 增加行间距 -// } -// )} -// //setTextColor(ContextCompat.getColor(context, R.color.black999)) -// } +//package com.shuwei.dish.match.adapter +// +//import android.graphics.Typeface +//import android.icu.text.DecimalFormat +//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 android.widget.FrameLayout +//import android.widget.TextView +//import androidx.core.content.ContextCompat +//import androidx.core.graphics.toColorInt +//import androidx.recyclerview.widget.RecyclerView +//import com.chad.library.adapter4.viewholder.QuickViewHolder +//import com.shuwei.dish.match.R +//import com.shuwei.dish.match.databinding.ListItemCellBinding +//import com.shuwei.dish.match.db.entity.SeasoningEntity +//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.clickWithDebounce +//import com.shuwei.dish.match.utils.ext.dp +//import com.shuwei.dish.match.utils.ext.invisible +//import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace +//import com.shuwei.dish.match.utils.ext.visible +// +//class TextCellAdapter(var list: MutableList) : +// RecyclerView.Adapter() { +// +// inner class VH(var binding: ListItemCellBinding) : QuickViewHolder(binding.root) +// +// override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { +// val inflater = LayoutInflater.from(parent.context) +// val binding = ListItemCellBinding.inflate(inflater, parent, false) +// return VH(binding) +// } +// +// override fun onBindViewHolder(holder: VH, position: Int) { +// val item = list[position] +// holder.binding.run { +// if (position == 10 || position == 11 || position == 13 || position == 14) { +// root.invisible() // } else { -//// if (item.pageType == 1) { -//// tvCell.run { -//// text = item.goodsName -//// setTextColor(ContextCompat.getColor(context, R.color.black)) -//// } -//// } else { -// tvCell.text = getTextSpan(item) -//// } +// root.visible() +// } +// loadCell(tvCell, item) +// //root.setBackgroundResource(R.color.dish_green) +// (root.layoutParams as RecyclerView.LayoutParams).let { +//// it.bottomMargin = 15.dp +// it.width = 226.dp +// it.height = 120.dp +//// it.rightMargin = 0 +//// it.leftMargin = if (position % 3 == 0) 0 else 15.dp +// +// it.leftMargin = 7.dp +// it.rightMargin = 8.dp +// it.topMargin = 7.dp +// it.bottomMargin = 8.dp +// } +// if (item.pageType == 1) { +// root.clickWithDebounce { onCellClick?.invoke(position) } // } // } - - fun getTextSpan(item: SeasoningEntity): SpannableStringBuilder { - val endIndex = if (item.sort == 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) - ) - } - } - } - -} \ No newline at end of file +// } +// +// override fun getItemCount() = list.size +// +// var onCellClick: ((positon: Int) -> Unit)? = null +// +// companion object { +// fun loadLayout(frameLayout: FrameLayout, item: SeasoningEntity) { +// val child: TextView = frameLayout.getChildAt(0) as TextView +// child.text = getTextSpan(item) +// } +// fun loadCell(tvCell: TextView, item: SeasoningEntity) { +// tvCell.text = getTextSpan(item) +// } +//// fun loadCell2(tvCell: TextView, item: SeasoningEntity) { +//// if (item.goodsName.isNullOrBlank()) { +//// tvCell.run { +//// text = buildSpannableString { +//// appendText( +//// "未配置", +//// ForegroundColorSpan("#999999".toColorInt()), +//// StyleSpan(Typeface.BOLD), +//// AbsoluteSizeSpan(28, true), +//// LineHeightSpan { text, start, end, spanstartv, v, fm -> +//// fm.descent += 10.dp // 增加行间距 +//// } +//// )} +//// //setTextColor(ContextCompat.getColor(context, R.color.black999)) +//// } +//// } else { +////// if (item.pageType == 1) { +////// tvCell.run { +////// text = item.goodsName +////// setTextColor(ContextCompat.getColor(context, R.color.black)) +////// } +////// } else { +//// tvCell.text = getTextSpan(item) +////// } +//// } +//// } +// +// fun getTextSpan(item: SeasoningEntity): SpannableStringBuilder { +// val endIndex = if (item.sort == 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) +// ) +// } +// } +// } +// +//} \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt index 79c5ccf..62a4592 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/DeviceConfigActivity.kt @@ -9,17 +9,12 @@ import android.text.style.LineHeightSpan import android.text.style.StyleSpan import android.util.Log import android.util.SparseArray +import android.widget.FrameLayout import androidx.core.graphics.toColorInt -import com.google.android.flexbox.FlexDirection -import com.google.android.flexbox.FlexboxLayoutManager -import com.google.android.flexbox.FlexWrap +import androidx.core.view.forEach 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.model.GoodsItem import com.shuwei.dish.match.utils.AddressUtil -import com.shuwei.dish.match.utils.MultiClickDetector import com.shuwei.dish.match.utils.WeightUtil import com.shuwei.dish.match.utils.ext.appendText import com.shuwei.dish.match.utils.ext.buildSpannableString @@ -27,10 +22,12 @@ 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.adapter.SeasoningAdapter import com.shuwei.dish.match.databinding.ActivityDeviceConfigBinding import com.shuwei.dish.match.dialog.BottomDialog2 +import com.shuwei.dish.match.model.GoodsItem import com.shuwei.dish.match.utils.SpTool +import com.shuwei.dish.match.utils.TextCellTool +import com.shuwei.dish.match.utils.ext.clickWithDebounce import com.shuwei.dish.match.utils.ext.gone class DeviceConfigActivity : BaseActivity() { @@ -56,7 +53,8 @@ class DeviceConfigActivity : BaseActivity() { private lateinit var binding: ActivityDeviceConfigBinding private var cookMode: Int = 0 -// private val weightArray = SparseIntArray() + + // private val weightArray = SparseIntArray() private val weightArray = SparseArray() private val addressArray = AddressUtil.getWeighAddressArray() @@ -117,7 +115,7 @@ class DeviceConfigActivity : BaseActivity() { finish() } addWeighListener() - setupRecyclerView() + addGridItemListener() loadSeasoning() } @@ -125,61 +123,56 @@ class DeviceConfigActivity : BaseActivity() { WeightUtil.addWeightListener( weightKey = TAG, 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) -// } - Log.d(TAG, "addWeighListener: update-----------------------") - }) + 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 + updateGridData(it) + } + Log.d(TAG, "addWeighListener: update-----------------------") + }) } - 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 var seasoningItems = mutableListOf() + + private fun updateGridData(entity: SeasoningEntity) { + val gridLayout = binding.include.root + val tag = entity.sort.toString() + val frameLayout = gridLayout.findViewWithTag(tag) + TextCellTool.loadLayout(frameLayout, entity) + } + + private fun addGridItemListener() { + binding.include.root.forEach { child -> + child.clickWithDebounce { + val sort = child.tag.toString().toInt() + val entity = seasoningItems.firstOrNull { it.sort == sort } + ?: SeasoningEntity().also { it.sort = sort } + clickGridItem(sort, entity) } } } - 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() { - val remindBinding = LayoutFoodRemindBinding.inflate(layoutInflater) - remindBinding.tvDialogTitle.text = "温馨提示" - remindBinding.tvDialogContent.text = - "您好,使用默认配置作为您的调料数据,则已有配置将被清除,确认吗?" - CommonDialog(this).apply { - messageView = remindBinding.root - leftText = "取消" - rightText = "确认" - onLeftClick = { dismiss() } - onRightClick = { - dismiss() - } - onDismiss = { hideStatusBar() } - }.show() - } - private fun loadSeasoning() { + Log.d(TAG, "loadSeasoning,start-----------------------") appViewModel.loadSeasoning { list -> + Log.d(TAG, "loadSeasoning,list.size=${list.size}-----------------------") seasoningItems.clear() - // 将 SeasoningEntity 转为 GoodsItem 后提交给 adapter - val goodsItems = list.map { it.toGoodsItem() } - seasoningAdapter.submitList(goodsItems) + // 补全缺失的格子:对 0~11 中没有对应 sort 的位置,追加空实体 + val existingSorts = list.map { it.sort }.toSet() + for (index in 0 until 12) { + if (index !in existingSorts) { + list.add(SeasoningEntity().also { it.sort = index }) + } + } + seasoningItems.addAll(list) + // 按 sort 字段找到对应 FrameLayout,填充数据 + list.forEach { entity -> + val tag = entity.sort.toString() + val frameLayout = binding.include.root.findViewWithTag(tag) + frameLayout?.let { TextCellTool.loadLayout(it, entity) } + } } } @@ -191,25 +184,11 @@ class DeviceConfigActivity : BaseActivity() { clickName = entity.goodsName ).show(this) { item -> syncItem(entity, item) - //3-调料 entity.materialType = 3 entity.sort = sort saveSeasoning(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) + val frameLayout = binding.include.root.findViewWithTag(sort.toString()) + frameLayout?.let { TextCellTool.loadLayout(it, entity) } } } } diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt index 2ef4a76..ed0a55e 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt @@ -6,11 +6,9 @@ import android.util.Log import android.util.SparseArray import android.util.SparseIntArray import android.widget.FrameLayout -import android.widget.TextView import androidx.core.util.forEach import com.google.gson.reflect.TypeToken import com.shuwei.dish.match.R -import com.shuwei.dish.match.adapter.TextCellAdapter import com.shuwei.dish.match.base.BaseActivity import com.shuwei.dish.match.base.BaseApp import com.shuwei.dish.match.databinding.ActivitySubmitFoodBinding @@ -26,6 +24,7 @@ import androidx.lifecycle.repeatOnLifecycle import com.shuwei.dish.match.model.toDTO import com.shuwei.dish.match.net.UiState import com.shuwei.dish.match.utils.AddressUtil +import com.shuwei.dish.match.utils.TextCellTool import kotlinx.coroutines.launch import com.shuwei.dish.match.utils.WeightUtil import com.shuwei.dish.match.utils.ext.clickWithDebounce @@ -222,7 +221,7 @@ class SubmitFoodActivity : BaseActivity() { val gridLayout = binding.include.root val tag = entity.sort.toString() val frameLayout = gridLayout.findViewWithTag(tag) - TextCellAdapter.loadLayout(frameLayout , entity) + TextCellTool.loadLayout(frameLayout , entity) } private fun getCookingSeasoning(address: Int): Double { @@ -444,7 +443,7 @@ class SubmitFoodActivity : BaseActivity() { list.forEach { entity -> val tag = entity.sort.toString() val frameLayout = gridLayout.findViewWithTag(tag) - TextCellAdapter.loadLayout(frameLayout, entity) + TextCellTool.loadLayout(frameLayout, entity) } } } diff --git a/app/src/main/java/com/shuwei/dish/match/utils/TextCellTool.kt b/app/src/main/java/com/shuwei/dish/match/utils/TextCellTool.kt new file mode 100644 index 0000000..deecd4e --- /dev/null +++ b/app/src/main/java/com/shuwei/dish/match/utils/TextCellTool.kt @@ -0,0 +1,56 @@ +package com.shuwei.dish.match.utils + +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.widget.FrameLayout +import android.widget.TextView +import androidx.core.graphics.toColorInt +import com.shuwei.dish.match.db.entity.SeasoningEntity +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 + +object TextCellTool { + + fun loadLayout(frameLayout: FrameLayout, item: SeasoningEntity) { + val child: TextView = frameLayout.getChildAt(0) as TextView + child.text = getTextSpan(item) + } + fun loadCell(tvCell: TextView, item: SeasoningEntity) { + tvCell.text = getTextSpan(item) + } + + fun getTextSpan(item: SeasoningEntity): SpannableStringBuilder { + val endIndex = if (item.sort == 9) 10 else 6 + var name = if (item.goodsName.isNullOrBlank()) "未配置" else item.goodsName!! + if (name.length > endIndex) { + name = "${name.take(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) + ) + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_device_config.xml b/app/src/main/res/layout/activity_device_config.xml index b86606e..5bad554 100644 --- a/app/src/main/res/layout/activity_device_config.xml +++ b/app/src/main/res/layout/activity_device_config.xml @@ -128,10 +128,11 @@ - + android:layout_height="wrap_content" + layout="@layout/layout_grid" />