diff --git a/app/src/main/java/com/shuwei/dish/match/ui/CookingModeActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/CookingModeActivity.kt index 84b243a..5376185 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/CookingModeActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/CookingModeActivity.kt @@ -3,6 +3,7 @@ package com.shuwei.dish.match.ui import android.content.Intent import android.os.Bundle import androidx.activity.addCallback +import androidx.core.widget.addTextChangedListener import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.launch import com.shuwei.dish.match.R @@ -92,6 +93,9 @@ class CookingModeActivity : BaseActivity() { .show() } + /** 切换餐次时清空输入框,用此标志跳过 afterTextChanged 的查询触发 */ + private var isClearingByTabSwitch = false + fun addViewListener() { binding.dishRadioGroup.setOnCheckedChangeListener { _, checkedId -> dinnerType = when (checkedId) { @@ -100,6 +104,10 @@ class CookingModeActivity : BaseActivity() { R.id.rbDinner -> "3" else -> "1" } + // 切换餐次时清空搜索框,避免用户误以为列表是被过滤的状态 + isClearingByTabSwitch = true + binding.etInputDish.text?.clear() + isClearingByTabSwitch = false loadFragment() } binding.etInputDish.addOnActionSearchListener { @@ -108,6 +116,17 @@ class CookingModeActivity : BaseActivity() { binding.ivDishSearch.setOnClickListener { 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() { @@ -116,10 +135,7 @@ class CookingModeActivity : BaseActivity() { toast(binding.etInputDish.hint.toString()) return } - startActivity { - putExtra(FoodSearchActivity.DINNER_TYPE, dinnerType) - putExtra(FoodSearchActivity.FOOD_NAME, searchText) - } + currentFragment()?.getDishList(pageNo = 1, dinnerType = dinnerType, foodName = searchText) hideKeyboard() } diff --git a/app/src/main/java/com/shuwei/dish/match/ui/fragment/FoodListFragment.kt b/app/src/main/java/com/shuwei/dish/match/ui/fragment/FoodListFragment.kt index dbc53b9..805705d 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/fragment/FoodListFragment.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/fragment/FoodListFragment.kt @@ -199,10 +199,10 @@ class FoodListFragment : BaseFragment() { } } - fun getDishList(pageNo: Int, dinnerType: String) { + fun getDishList(pageNo: Int, dinnerType: String, foodName: String? = null) { this.pageNo = pageNo this.dinnerType = dinnerType - getDishList() + getDishList(foodName) } fun getDinnerTypeText(): String { @@ -216,13 +216,15 @@ class FoodListFragment : BaseFragment() { @SuppressLint("NotifyDataSetChanged") @Suppress("unchecked_cast") - fun getDishList() { + fun getDishList(foodName: String? = null) { val param = mutableMapOf( "pageNum" to pageNo, "pageSize" to pageSize, "placeId" to BaseApp.canteenId, "dinnerType" to getDinnerTypeText() ) + // foodName 不为空时按名称过滤,为 null 时查全部 + foodName?.let { param["foodName"] = it } activity.getFoodList(param = param) if (pageNo == 1) activity.getCookFoodList() }