From 67d64848de92ad5177a0e718a753b3f6eb8c1d2f Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Wed, 13 May 2026 16:22:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(activity):=20=E6=90=9C=E7=B4=A2=E8=8F=9C?= =?UTF-8?q?=E5=93=81=E7=9B=B4=E6=8E=A5=E5=88=B7=E6=96=B0=E5=88=97=E8=A1=A8?= =?UTF-8?q?=EF=BC=8C=E5=88=87=E6=8D=A2=E9=A4=90=E6=AC=A1=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=B8=85=E7=A9=BA=E6=90=9C=E7=B4=A2=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - jumpSearch 改为调用 FoodListFragment.getDishList(foodName),不再跳转 FoodSearchActivity - 切换餐次时清空输入框,用 isClearingByTabSwitch 标志避免重复请求 - 手动清空输入框时自动触发查全部数据 - FoodListFragment.getDishList 新增 foodName 参数,支持按名称过滤 Co-Authored-By: Claude Sonnet 4.6 --- .../dish/match/ui/CookingModeActivity.kt | 24 +++++++++++++++---- .../match/ui/fragment/FoodListFragment.kt | 8 ++++--- 2 files changed, 25 insertions(+), 7 deletions(-) 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() }