refactor(collect): 重构食材采集功能界面和适配器

- 将按钮组件替换为TextView以支持更好的交互效果
- 添加selectableItemBackground实现点击反馈效果
- 重命名食材采样为食材采集保持术语一致性
- 替换FoodCollectionAdapter为VectorCollectionAdapter适配器
- 更新列表项布局从list_item_food_collection到list_item_vector_collection
- 添加FoodSearchAdapter适配器用于食材搜索功能
- 重构CollectFragment中的数据绑定和UI逻辑
- 修改相机容器布局位置优化界面结构
- 更新资源文件名和颜色定义提升代码可读性
- 调整保存按钮样式增强视觉效果
- 修改提示对话框内容精确表达功能意图
This commit is contained in:
2026-04-27 10:21:37 +08:00
parent fda88fff33
commit b110cc97a0
16 changed files with 141 additions and 68 deletions
@@ -0,0 +1,43 @@
package com.shuwei.dish.match.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.shuwei.dish.match.R
import com.shuwei.dish.match.databinding.ListItemSearchFoodBinding
import com.shuwei.dish.match.databinding.ListItemSearchGoodsInfoBinding
import com.shuwei.dish.match.entity.CookFoodGoodsEntity
import com.shuwei.dish.match.utils.ext.gone
import com.shuwei.dish.match.utils.ext.roundedDecimalPlace
import com.shuwei.dish.match.utils.ext.visible
class FoodSearchAdapter(private var list: MutableList<CookFoodGoodsEntity>) :
BaseQuickAdapter<CookFoodGoodsEntity, FoodSearchAdapter.VH>(list) {
override fun onBindViewHolder(
holder: VH,
position: Int,
item: CookFoodGoodsEntity?
) {
item ?: return
holder.binding.tvFoodName.run {
text = item.goodsName
isChecked = item.isClicked
}
}
override fun onCreateViewHolder(
context: Context,
parent: ViewGroup,
viewType: Int
): VH {
val binding = ListItemSearchFoodBinding.inflate(LayoutInflater.from(context), parent, false)
return VH(binding)
}
inner class VH(var binding: ListItemSearchFoodBinding) : QuickViewHolder(binding.root)
}
@@ -8,18 +8,18 @@ import android.widget.ImageView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.shuwei.dish.match.R
import com.shuwei.dish.match.databinding.ListItemFoodCollectionBinding
import com.shuwei.dish.match.databinding.ListItemVectorCollectionBinding
import com.shuwei.dish.match.objbox.FoodCollectionBean
import com.shuwei.dish.match.utils.ext.load
class FoodCollectionAdapter (var list: MutableList<FoodCollectionBean>) :
BaseQuickAdapter<FoodCollectionBean, FoodCollectionAdapter.VH>(list) {
class VectorCollectionAdapter (var list: MutableList<FoodCollectionBean>) :
BaseQuickAdapter<FoodCollectionBean, VectorCollectionAdapter.VH>(list) {
inner class VH(var binding: ListItemFoodCollectionBinding) : QuickViewHolder(binding.root)
inner class VH(var binding: ListItemVectorCollectionBinding) : QuickViewHolder(binding.root)
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
val inflater = LayoutInflater.from(context)
val binding = ListItemFoodCollectionBinding.inflate(inflater, parent, false)
val binding = ListItemVectorCollectionBinding.inflate(inflater, parent, false)
return VH(binding)
}
@@ -265,8 +265,8 @@ class PrepareCookActivity : BaseActivity() {
private fun showRemindDialog() {
CommonDialog(this)
.setTitle("制作提示")
.setContent(getString(R.string.food_remind_01))
.setTitle(getString(R.string.food_remind_01))
.setContent(getString(R.string.food_remind_02))
.setNegativeButton("返回调整")
.setPositiveButton("确认无误") { openSubmitPage() }
.setOnDismissCallback { hideStatusBar() }
@@ -116,7 +116,7 @@ class SettingActivity : BaseActivity() {
private fun showNotGrantedCameraPermissionDialog() {
CommonDialog(this)
.setTitle("温馨提示")
.setContent("暂无相机权限,无法使用菜品采集功能,请确认是否同意开始权限?")
.setContent("暂无相机权限,无法使用食材采集功能,请确认是否同意开始权限?")
.setNegativeButton("不同意")
.setPositiveButton("同意") {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
@@ -94,7 +94,7 @@ class SingleFragmentActivity : BaseActivity() {
* 根据页面类型返回对应标题文本
*/
private fun getTitleForPage(pageType: PageType): String = when (pageType) {
PageType.FOOD_COLLECT -> "食材采"
PageType.FOOD_COLLECT -> "食材采"
PageType.COOK_MODE -> "餐品模式"
PageType.SEASONING_CONFIG -> "调料区设置"
}
@@ -16,11 +16,13 @@ import androidx.core.view.doOnLayout
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.GridLayoutManager
import com.shuwei.dish.match.R
import com.shuwei.dish.match.adapter.FoodCollectionAdapter
import com.shuwei.dish.match.adapter.VectorCollectionAdapter
import com.shuwei.dish.match.adapter.FoodSearchAdapter
import com.shuwei.dish.match.base.BaseFragment
import com.shuwei.dish.match.databinding.FragmentCollectBinding
import com.shuwei.dish.match.databinding.LayoutCameraPreviewBinding
import com.shuwei.dish.match.dialog.Loading
import com.shuwei.dish.match.entity.CookFoodGoodsEntity
import com.shuwei.dish.match.objbox.FoodCollectionBean
import com.shuwei.dish.match.objbox.FoodModule
import com.shuwei.dish.match.ui.CollectedFoodActivity
@@ -35,7 +37,7 @@ import com.shuwei.dish.match.utils.ext.dp
import com.shuwei.dish.match.utils.ext.toast
@SuppressLint("NotifyDataSetChanged")
class CollectFragment : BaseFragment<FragmentCollectBinding>(){
class CollectFragment : BaseFragment<FragmentCollectBinding>() {
companion object {
private const val TAG = "CollectFragment"
@@ -45,34 +47,47 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
private var selectedFoodId: String? = ""
private var selectedFoodName: String? = ""
private val foodCollectionList = mutableListOf<FoodCollectionBean>().apply {
private val vectorList = mutableListOf<FoodCollectionBean>().apply {
repeat(MAX_COUNT) {
add(FoodCollectionBean(isShowCamera = true))
}
}
// private val searchFoodList = mutableListOf<FoodInfo>()
// private var settingActivity: SettingActivity? = null
// private var checkedItem: FoodInfo? = null
// private val searchFoodAdapter by lazy {
// CollectFoodListAdapter(searchFoodList).apply {
// setOnItemClickListener { adapter, view, position ->
// searchFoodList.forEachIndexed { index, item -> item.isChecked = index == position }
// checkedItem = searchFoodList[position]
// notifyDataSetChanged()
// selectedFoodId = checkedItem!!.foodId
// selectedFoodName = checkedItem!!.foodName
// }
// }
// }
private val searchList = mutableListOf<CookFoodGoodsEntity>().apply {
add(CookFoodGoodsEntity(goodsId = "200001", goodsName = "土豆丝"))
add(CookFoodGoodsEntity(goodsId = "200002", goodsName = "土豆片"))
add(CookFoodGoodsEntity(goodsId = "200003", goodsName = "土豆丁"))
add(CookFoodGoodsEntity(goodsId = "200004", goodsName = "胡萝卜丝"))
add(CookFoodGoodsEntity(goodsId = "200005", goodsName = "胡萝卜片"))
add(CookFoodGoodsEntity(goodsId = "200006", goodsName = "胡萝卜丁"))
add(CookFoodGoodsEntity(goodsId = "200007", goodsName = "黄瓜丝"))
add(CookFoodGoodsEntity(goodsId = "200008", goodsName = "黄瓜片"))
add(CookFoodGoodsEntity(goodsId = "200009", goodsName = "黄瓜丁"))
add(CookFoodGoodsEntity(goodsId = "200010", goodsName = "洋葱丝"))
add(CookFoodGoodsEntity(goodsId = "200011", goodsName = "洋葱丁"))
}
private var checkedItem: CookFoodGoodsEntity? = null
private val searchAdapter by lazy {
FoodSearchAdapter(searchList).apply {
setOnItemClickListener { adapter, view, position ->
searchList.forEachIndexed { index, item -> item.isClicked = index == position }
checkedItem = searchList[position]
notifyDataSetChanged()
checkedItem?.let {
selectedFoodId = it.foodId
selectedFoodName = it.goodsName
}
}
}
}
private val debouncer = Debouncer(2000)
// private lateinit var settingActivity: SettingActivity
private lateinit var currentActivity: SingleFragmentActivity
private val collectionAdapter: FoodCollectionAdapter by lazy {
FoodCollectionAdapter(foodCollectionList).apply {
private val vectorAdapter: VectorCollectionAdapter by lazy {
VectorCollectionAdapter(vectorList).apply {
addOnItemChildClickListener(R.id.ivDelete) { _, _, position ->
foodCollectionList[position].let {
vectorList[position].let {
it.bitmap = null
it.imageVector = null
it.imageFile = null
@@ -96,23 +111,23 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
//typealias CameraCallback = (Uri) -> Unit
private val cameraCallback: (Uri) -> Unit = { uri ->
private val cameraCallback: (Uri) -> Unit = cameraCallback@{ uri ->
try {
val index = foodCollectionList.indexOfFirst { it.imageFile == null }
val index = vectorList.indexOfFirst { it.imageFile == null }
if (index == -1) {
toast("每次只允许保存${MAX_COUNT}条数据")
hideWaitingDialog()
rerurn@ cameraCallback
return@cameraCallback
}
activity?.runOnUiThread {
foodCollectionList[index].let {
vectorList[index].let {
it.imageVector = null
it.bitmap = null
it.isShowCamera = false
it.imageFile = null
it.imageUri = uri
}
collectionAdapter.notifyItemChanged(index)
vectorAdapter.notifyItemChanged(index)
}
// hideWaitingDialog()
Thread {
@@ -149,13 +164,13 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
log("${this.javaClass.simpleName}-cameraCallback-裁剪bitmap保存文件路径:${file?.absolutePath}")
activity?.runOnUiThread {
foodCollectionList[index].let {
vectorList[index].let {
it.imageVector = imageVector
it.bitmap = null
it.isShowCamera = false
it.imageFile = file
}
collectionAdapter.notifyItemChanged(index)
vectorAdapter.notifyItemChanged(index)
}
if (bitmap.isRecycled.not()) {
bitmap.recycle()
@@ -165,12 +180,12 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
@SuppressLint("NotifyDataSetChanged")
private fun takePhoto() {
val count = foodCollectionList.count { it.bitmap != null }
val count = vectorList.count { it.bitmap != null }
if (count >= MAX_COUNT) {
toast("每次只允许保存${MAX_COUNT}条数据")
return
}
val index = foodCollectionList.indexOfFirst { it.imageFile == null }
val index = vectorList.indexOfFirst { it.imageFile == null }
if (index == -1) {
toast("每次只允许保存${MAX_COUNT}条数据")
hideWaitingDialog()
@@ -212,7 +227,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
binding.rvFoodList.let {
it.layoutManager =
GridLayoutManager(requireActivity(), 3, GridLayoutManager.VERTICAL, false)
it.adapter = collectionAdapter
it.adapter = vectorAdapter
}
binding.btnFoodSearch.setOnClickListener {
@@ -248,7 +263,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
startActivity(Intent(requireActivity(), CollectedFoodActivity::class.java))
}
binding.btnTakePhoto.clickWithDebounce {
val count = foodCollectionList.count { it.bitmap != null }
val count = vectorList.count { it.bitmap != null }
if (count >= MAX_COUNT) {
toast("每次只允许保存${MAX_COUNT}条数据")
return@clickWithDebounce
@@ -256,10 +271,10 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
takePhoto()
}
binding.btnClearData.setOnClickListener { clearData() }
// binding.rvSearchFood.let {
// it.layoutManager = GridLayoutManager(context, 2)
// it.adapter = searchFoodAdapter
// }
binding.rvSearchFood.let {
it.layoutManager = GridLayoutManager(context, 2)
it.adapter = searchAdapter
}
searchFood()
}
@@ -342,7 +357,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
@SuppressLint("NotifyDataSetChanged")
fun clearData() {
foodCollectionList.forEach {
vectorList.forEach {
it.bitmap = null
it.imageVector = null
it.imageFile = null
@@ -351,7 +366,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
it.isFinish = false
it.uploadSuccess = false
}
collectionAdapter.notifyDataSetChanged()
vectorAdapter.notifyDataSetChanged()
clickIndex = -1
binding.editFoodName.setText("")
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/dish_green" android:state_checked="true" />
<item android:color="#5E7585" android:state_checked="false" />
</selector>
@@ -4,14 +4,14 @@
<shape android:shape="rectangle">
<corners android:radius="50dp"/>
<solid android:color="@color/white"/>
<!-- <stroke android:color="#FFC4CFDA" android:width="2dp"/>-->
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="50dp"/>
<solid android:color="#FFFFF0F0"/>
<stroke android:color="#FFFF3232" android:width="2dp"/>
<!-- <solid android:color="#FFFFF0F0"/>-->
<solid android:color="#F0FFF0"/>
<stroke android:color="@color/dish_green" android:width="2dp"/>
</shape>
</item>
</selector>
@@ -271,7 +271,7 @@
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
<TextView
android:id="@+id/btnCook"
android:layout_width="match_parent"
android:layout_height="90dp"
@@ -282,6 +282,7 @@
android:textSize="32sp"
android:textStyle="bold"
android:background="@drawable/shape_green_bg"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true" />
<!-- android:background="@drawable/ripple_effect_green"-->
@@ -65,6 +65,7 @@
android:layout_marginTop="30dp"
android:gravity="center"
android:background="@drawable/shape_green_bg"
android:foreground="?android:attr/selectableItemBackground"
android:text="更换食材"
android:textColor="@color/white"
android:textSize="32sp"
@@ -79,6 +80,7 @@
android:layout_marginBottom="30dp"
android:gravity="center"
android:background="@drawable/shape_white_12_corners"
android:foreground="?android:attr/selectableItemBackground"
android:text="取消"
android:textColor="@color/black333"
android:textSize="32sp"
@@ -75,6 +75,11 @@
android:textColor="@color/black666"
android:textSize="28sp" />
<FrameLayout
android:id="@+id/flCameraContainer"
android:layout_width="1dp"
android:layout_height="1dp" />
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
@@ -89,17 +94,13 @@
</LinearLayout>
<FrameLayout
android:id="@+id/flCameraContainer"
android:layout_width="1dp"
android:layout_height="1dp" />
<androidx.appcompat.widget.AppCompatButton
<TextView
android:id="@+id/btnCook"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_margin="30dp"
android:background="@drawable/shape_green_bg"
android:foreground="?android:attr/selectableItemBackground"
android:gravity="center"
android:text="@string/goCooking"
android:textColor="@color/white"
+1 -1
View File
@@ -94,7 +94,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="食材采"
android:text="食材采"
android:textSize="32sp"
android:textStyle="bold"
android:layout_marginStart="32dp"
@@ -112,7 +112,7 @@
android:gravity="center_vertical"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
<TextView
android:id="@+id/btnCook"
android:layout_width="0dp"
android:layout_height="90dp"
@@ -121,6 +121,7 @@
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:background="@drawable/shape_green_stroke"
android:foreground="?android:attr/selectableItemBackground"
android:gravity="center"
android:text="制作"
android:textColor="@color/dish_green"
@@ -128,7 +129,7 @@
android:textStyle="bold" />
<!-- android:background="@drawable/ripple_effect_light"-->
<androidx.appcompat.widget.AppCompatButton
<TextView
android:id="@+id/btnSubmit"
android:layout_width="0dp"
android:layout_height="90dp"
@@ -138,6 +139,7 @@
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:background="@drawable/shape_green_bg"
android:foreground="?android:attr/selectableItemBackground"
android:gravity="center"
android:text="提交"
android:textColor="@color/white"
+9 -5
View File
@@ -139,7 +139,7 @@
app:spanCount="3"
tools:itemCount="12"
android:overScrollMode="never"
tools:listitem="@layout/list_item_food_collection" />
tools:listitem="@layout/list_item_vector_collection" />
</androidx.constraintlayout.widget.ConstraintLayout>
@@ -157,9 +157,9 @@
android:layout_height="match_parent"
android:background="@drawable/setting_border_gray3"
android:gravity="center_vertical"
android:hint="输入菜品名称"
android:inputType="text"
android:hint="输入食材名称"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:paddingStart="47dp"
android:paddingEnd="80dp"
@@ -191,7 +191,7 @@
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="@layout/list_item_search_food"/>
<androidx.appcompat.widget.AppCompatButton
<TextView
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="100dp"
@@ -199,8 +199,12 @@
android:layout_marginBottom="32dp"
android:layout_marginTop="20dp"
android:text="保存"
android:gravity="center"
android:textStyle="bold"
android:foreground="?android:attr/selectableItemBackground"
android:background="@drawable/bg_btn_save"
android:textColor="@color/white"
android:textSize="40sp" />
android:textSize="40sp"
android:clipToOutline="true"/>
</LinearLayout>
@@ -4,7 +4,7 @@
android:id="@+id/tvFoodName"
android:layout_width="match_parent"
android:layout_height="100dp"
tools:background="@drawable/bg_text_main"
android:background="@drawable/bg_item_search"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
@@ -12,7 +12,7 @@
android:layout_marginVertical="14dp"
android:paddingHorizontal="15dp"
android:textAlignment="center"
tools:textColor="@color/black"
android:textColor="@color/color_item_search"
android:textSize="30sp"
android:textStyle="bold"
tools:text="肉沫干拌面" />
tools:text="土豆丝" />