fix(ui): 修复搜索列表状态管理和界面交互问题
- 添加了 clipToOutline 和 foreground 属性以改善列表项视觉效果 - 修复了 UiState 处理中未重置商品列表状态的问题 - 优化了 searchFood 方法中的状态重置逻辑,避免重复调用 - 修复了空视图点击监听器设置时机,确保在状态视图挂载完成后添加监听器 - 移除了重复的搜索调用,解决因 setText 触发的多次搜索问题
This commit is contained in:
@@ -313,11 +313,13 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
|
|||||||
is UiState.Success -> {
|
is UiState.Success -> {
|
||||||
val list = state.data ?: emptyList()
|
val list = state.data ?: emptyList()
|
||||||
if (list.isEmpty()) loadEmptyView() else fillSearchList(list)
|
if (list.isEmpty()) loadEmptyView() else fillSearchList(list)
|
||||||
|
currentActivity.netViewModel.resetGoodsListState()
|
||||||
}
|
}
|
||||||
|
|
||||||
is UiState.Error -> {
|
is UiState.Error -> {
|
||||||
toast(state.msg)
|
toast(state.msg)
|
||||||
loadEmptyView()
|
loadEmptyView()
|
||||||
|
currentActivity.netViewModel.resetGoodsListState()
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> Unit
|
else -> Unit
|
||||||
@@ -383,16 +385,15 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
|
|||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
fun searchFood() {
|
fun searchFood() {
|
||||||
|
log("searchFood")
|
||||||
|
// 立即重置,防止 StateFlow 向新订阅者重播旧的错误状态
|
||||||
|
currentActivity.netViewModel.resetGoodsListState()
|
||||||
debouncer.debounce {
|
debouncer.debounce {
|
||||||
val keyword = binding.editFoodName.text.toString().trim()
|
val keyword = binding.editFoodName.text.toString().trim()
|
||||||
// 通过 NetViewModel 查询净材数据(goodsType=0 食材),结果由 initialize() 中的 observer 统一处理
|
currentActivity.netViewModel.queryGoodsList(
|
||||||
currentActivity.netViewModel.run {
|
goodsType = "0",
|
||||||
resetGoodsListState()
|
goodsName = keyword.ifEmpty { null }
|
||||||
queryGoodsList(
|
)
|
||||||
goodsType = "0",
|
|
||||||
goodsName = keyword.ifEmpty { null }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,14 +446,12 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
|
|||||||
emptyViewBinding?.let {
|
emptyViewBinding?.let {
|
||||||
it.tvContent.text = "暂无数据"
|
it.tvContent.text = "暂无数据"
|
||||||
it.tvSubContent.text = "未查询到食材信息,请稍后重试"
|
it.tvSubContent.text = "未查询到食材信息,请稍后重试"
|
||||||
// 点击空布局
|
|
||||||
it.root.setOnClickListener {
|
|
||||||
//填充本地假数据,便于 UI 效果测试
|
|
||||||
//fillSearchList(buildMockGoodsList())
|
|
||||||
searchFood()
|
|
||||||
}
|
|
||||||
binding.rvSearchFood.post {
|
binding.rvSearchFood.post {
|
||||||
it.root.minimumHeight = binding.rvSearchFood.height
|
it.root.minimumHeight = binding.rvSearchFood.height
|
||||||
|
// 在 stateView 挂载完成后再设置点击监听,避免 adapter 重置 view 状态时丢失监听器
|
||||||
|
it.root.setOnClickListener {
|
||||||
|
searchFood()
|
||||||
|
}
|
||||||
searchAdapter.stateView = it.root
|
searchAdapter.stateView = it.root
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -477,8 +476,8 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
|
|||||||
vectorAdapter.notifyDataSetChanged()
|
vectorAdapter.notifyDataSetChanged()
|
||||||
|
|
||||||
clickIndex = -1
|
clickIndex = -1
|
||||||
|
// setText("") 会触发 addTextChangedListener → searchFood(),无需再次直接调用
|
||||||
binding.editFoodName.setText("")
|
binding.editFoodName.setText("")
|
||||||
searchFood()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private var isFirstOpen = true
|
private var isFirstOpen = true
|
||||||
|
|||||||
@@ -19,7 +19,9 @@
|
|||||||
android:textSize="28sp"
|
android:textSize="28sp"
|
||||||
android:textStyle="bold"
|
android:textStyle="bold"
|
||||||
android:background="@drawable/bg_item_search"
|
android:background="@drawable/bg_item_search"
|
||||||
tools:text="土豆丝" />
|
tools:text="土豆丝"
|
||||||
|
android:clipToOutline="true"
|
||||||
|
android:foreground="?android:attr/selectableItemBackground"/>
|
||||||
|
|
||||||
<!-- 当前选中的净材种类,小字显示,无数据时隐藏 -->
|
<!-- 当前选中的净材种类,小字显示,无数据时隐藏 -->
|
||||||
<TextView
|
<TextView
|
||||||
|
|||||||
Reference in New Issue
Block a user