| | |
| | | import com.xw.yunwei.model.Item |
| | | import kotlin.collections.filter |
| | | |
| | | /** |
| | | * 事故类型筛选右侧子类 Adapter |
| | | * |
| | | * 用于事故上报页面的底部筛选弹窗,展示当前选中大类下的具体子项列表。 |
| | | * 支持多选效果:选中项蓝色背景 + 蓝色文字,未选中项透明背景 + 深灰文字。 |
| | | * |
| | | * @param items 子类数据列表 |
| | | * @param onItemClick 子项点击回调,参数为当前点击的 [Item] 及其下标,可为 null |
| | | */ |
| | | class RightItemAdapter( |
| | | private var items: List<Item>, |
| | | private val onItemClick: ((Item, Int) -> Unit)? = null |
| | | ) : RecyclerView.Adapter<RightItemAdapter.ViewHolder>() { |
| | | |
| | | /** |
| | | * 右侧子类 ViewHolder |
| | | * |
| | | * @property tvItem 子项名称文本控件 |
| | | */ |
| | | class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { |
| | | val tvItem: TextView = itemView.findViewById(R.id.tvRight) |
| | | } |
| | |
| | | val item = items[position] |
| | | holder.tvItem.text = item.name |
| | | |
| | | // 设置选中效果 |
| | | // 设置选中效果:选中为蓝色圆角背景 + 蓝色文字,未选中为半透明背景 + 深灰文字 |
| | | if (item.isSelected) { |
| | | holder.tvItem.setBackgroundResource(R.drawable.shape_blue_1_bg_4) |
| | | holder.tvItem.setTextColor(getResColor(com.xw.common.R.color.blue_5)) |
| | |
| | | holder.tvItem.setTextColor(getResColor(com.xw.common.R.color.gray_9)) |
| | | } |
| | | |
| | | // 点击切换选中状态并局部刷新 |
| | | holder.itemView.setOnClickListener { |
| | | // 切换选中状态 |
| | | item.isSelected = !item.isSelected |
| | |
| | | |
| | | override fun getItemCount() = items.size |
| | | |
| | | /** |
| | | * 更新子类数据并刷新列表 |
| | | * |
| | | * 通常在左侧大类切换时调用,用于展示新选中大类对应的子项。 |
| | | * |
| | | * @param newItems 新的子类数据列表 |
| | | */ |
| | | @SuppressLint("NotifyDataSetChanged") |
| | | fun updateData(newItems: List<Item>) { |
| | | items = newItems |
| | | notifyDataSetChanged() |
| | | } |
| | | |
| | | /** |
| | | * 获取当前已选中的子项列表 |
| | | * |
| | | * @return 所有 [Item.isSelected] 为 true 的子项 |
| | | */ |
| | | fun getSelectedItems(): List<Item> { |
| | | return items.filter { it.isSelected } |
| | | } |