feat(activity): 搜索菜品直接刷新列表,切换餐次自动清空搜索框
- jumpSearch 改为调用 FoodListFragment.getDishList(foodName),不再跳转 FoodSearchActivity - 切换餐次时清空输入框,用 isClearingByTabSwitch 标志避免重复请求 - 手动清空输入框时自动触发查全部数据 - FoodListFragment.getDishList 新增 foodName 参数,支持按名称过滤 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package com.shuwei.dish.match.ui
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.addCallback
|
import androidx.activity.addCallback
|
||||||
|
import androidx.core.widget.addTextChangedListener
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import com.shuwei.dish.match.R
|
import com.shuwei.dish.match.R
|
||||||
@@ -92,6 +93,9 @@ class CookingModeActivity : BaseActivity() {
|
|||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 切换餐次时清空输入框,用此标志跳过 afterTextChanged 的查询触发 */
|
||||||
|
private var isClearingByTabSwitch = false
|
||||||
|
|
||||||
fun addViewListener() {
|
fun addViewListener() {
|
||||||
binding.dishRadioGroup.setOnCheckedChangeListener { _, checkedId ->
|
binding.dishRadioGroup.setOnCheckedChangeListener { _, checkedId ->
|
||||||
dinnerType = when (checkedId) {
|
dinnerType = when (checkedId) {
|
||||||
@@ -100,6 +104,10 @@ class CookingModeActivity : BaseActivity() {
|
|||||||
R.id.rbDinner -> "3"
|
R.id.rbDinner -> "3"
|
||||||
else -> "1"
|
else -> "1"
|
||||||
}
|
}
|
||||||
|
// 切换餐次时清空搜索框,避免用户误以为列表是被过滤的状态
|
||||||
|
isClearingByTabSwitch = true
|
||||||
|
binding.etInputDish.text?.clear()
|
||||||
|
isClearingByTabSwitch = false
|
||||||
loadFragment()
|
loadFragment()
|
||||||
}
|
}
|
||||||
binding.etInputDish.addOnActionSearchListener {
|
binding.etInputDish.addOnActionSearchListener {
|
||||||
@@ -108,6 +116,17 @@ class CookingModeActivity : BaseActivity() {
|
|||||||
binding.ivDishSearch.setOnClickListener {
|
binding.ivDishSearch.setOnClickListener {
|
||||||
jumpSearch()
|
jumpSearch()
|
||||||
}
|
}
|
||||||
|
// 输入框清空时自动查询全部数据(切换餐次触发的清空除外,由 loadFragment 负责刷新)
|
||||||
|
binding.etInputDish.addTextChangedListener(afterTextChanged = { text ->
|
||||||
|
if (!isClearingByTabSwitch && text.isNullOrBlank()) {
|
||||||
|
currentFragment()?.getDishList(pageNo = 1, dinnerType = dinnerType)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 获取当前显示的 FoodListFragment */
|
||||||
|
private fun currentFragment(): FoodListFragment? {
|
||||||
|
return runCatching { fragmentList[dinnerType.toInt() - 1] }.getOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun jumpSearch() {
|
private fun jumpSearch() {
|
||||||
@@ -116,10 +135,7 @@ class CookingModeActivity : BaseActivity() {
|
|||||||
toast(binding.etInputDish.hint.toString())
|
toast(binding.etInputDish.hint.toString())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
startActivity<FoodSearchActivity> {
|
currentFragment()?.getDishList(pageNo = 1, dinnerType = dinnerType, foodName = searchText)
|
||||||
putExtra(FoodSearchActivity.DINNER_TYPE, dinnerType)
|
|
||||||
putExtra(FoodSearchActivity.FOOD_NAME, searchText)
|
|
||||||
}
|
|
||||||
hideKeyboard()
|
hideKeyboard()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -199,10 +199,10 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getDishList(pageNo: Int, dinnerType: String) {
|
fun getDishList(pageNo: Int, dinnerType: String, foodName: String? = null) {
|
||||||
this.pageNo = pageNo
|
this.pageNo = pageNo
|
||||||
this.dinnerType = dinnerType
|
this.dinnerType = dinnerType
|
||||||
getDishList()
|
getDishList(foodName)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getDinnerTypeText(): String {
|
fun getDinnerTypeText(): String {
|
||||||
@@ -216,13 +216,15 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
|||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
@Suppress("unchecked_cast")
|
@Suppress("unchecked_cast")
|
||||||
fun getDishList() {
|
fun getDishList(foodName: String? = null) {
|
||||||
val param = mutableMapOf<String, Any>(
|
val param = mutableMapOf<String, Any>(
|
||||||
"pageNum" to pageNo,
|
"pageNum" to pageNo,
|
||||||
"pageSize" to pageSize,
|
"pageSize" to pageSize,
|
||||||
"placeId" to BaseApp.canteenId,
|
"placeId" to BaseApp.canteenId,
|
||||||
"dinnerType" to getDinnerTypeText()
|
"dinnerType" to getDinnerTypeText()
|
||||||
)
|
)
|
||||||
|
// foodName 不为空时按名称过滤,为 null 时查全部
|
||||||
|
foodName?.let { param["foodName"] = it }
|
||||||
activity.getFoodList(param = param)
|
activity.getFoodList(param = param)
|
||||||
if (pageNo == 1) activity.getCookFoodList()
|
if (pageNo == 1) activity.getCookFoodList()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user