我的家庭切换
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
package com.example.defenseapplication.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.bean.FamilyBean
|
||||
import com.example.defenseapplication.databinding.ItemFamilyBinding
|
||||
|
||||
class FamilyAdapter(
|
||||
private val onItemClick: (String) -> Unit
|
||||
) : RecyclerView.Adapter<FamilyAdapter.FamilyViewHolder>() {
|
||||
|
||||
private val items = mutableListOf<String>()
|
||||
private val items = mutableListOf<FamilyBean>()
|
||||
private var selectedFamily: String? = null
|
||||
|
||||
fun submitList(familyList: List<String>, currentFamily: String) {
|
||||
fun submitList(familyList: List<FamilyBean>, currentFamily: String) {
|
||||
items.clear()
|
||||
items.addAll(familyList)
|
||||
selectedFamily = currentFamily
|
||||
@@ -34,11 +36,36 @@ class FamilyAdapter(
|
||||
inner class FamilyViewHolder(private val binding: ItemFamilyBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
fun bind(item: String) {
|
||||
binding.tvFamilyName.text = item
|
||||
val textColor = if (item == selectedFamily) R.color.green else R.color.black
|
||||
binding.tvFamilyName.setTextColor(binding.root.context.getColor(textColor))
|
||||
binding.root.setOnClickListener { onItemClick(item) }
|
||||
fun bind(item: FamilyBean) {
|
||||
binding.tvFamilyName.text = item.parentName
|
||||
binding.tvFamilyRole.text = getRoleText(item.role)
|
||||
|
||||
val isSelected = item.parentName == selectedFamily
|
||||
updateSelectedState(isSelected)
|
||||
|
||||
binding.root.setOnClickListener { onItemClick(item.parentName) }
|
||||
}
|
||||
|
||||
private fun getRoleText(role: String): String {
|
||||
return when (role) {
|
||||
"appadmin" ->binding.root.context.getString(R.string.admin_user)
|
||||
else -> binding.root.context.getString(R.string.normal_user)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateSelectedState(isSelected: Boolean) {
|
||||
if (isSelected) {
|
||||
binding.layoutFamilyItem.setBackgroundResource(R.drawable.bg_family_selected)
|
||||
binding.tvFamilyName.setTextColor(binding.root.context.getColor(R.color.white))
|
||||
binding.tvFamilyRole.setTextColor(binding.root.context.getColor(R.color.white))
|
||||
binding.ivCheck.visibility = View.VISIBLE
|
||||
binding.ivCheck.setImageResource(R.drawable.ic_check_white)
|
||||
} else {
|
||||
binding.layoutFamilyItem.setBackgroundResource(0)
|
||||
binding.tvFamilyName.setTextColor(binding.root.context.getColor(R.color.black))
|
||||
binding.tvFamilyRole.setTextColor(binding.root.context.getColor(R.color.gray))
|
||||
binding.ivCheck.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user