如何显示在 recyclerview 中选中的总复选框
How to display total checkbox checked in a recyclerview
我正在尝试创建一个简单的图鉴,您可以在其中跟踪收集了多少只宠物小精灵。我遇到的麻烦是 recyclerview 中的复选框。我想让当前为零的总数,选中每个复选框。如果未选中,该数字将下降。总数只是简单地显示在文本视图中。
下图显示了我要解释的内容,以防我没有解释清楚。
这是我的代码
适配器Class
class MainAdapter(val pokeList : List<PokeData>) : RecyclerView.Adapter<MainAdapter.ViewHolder>() {
var totalCollectedPokemon = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater
.inflate(R.layout.poke_contents, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int = pokeList.size
override fun onBindViewHolder(holder: MainAdapter.ViewHolder, position: Int) {
val pokeDetails = pokeList[position]
holder.checkbox.setOnClickListener {
if(holder.checkbox.isChecked) {
totalCollectedPokemon = totalCollectedPokemon + 1
} else {
totalCollectedPokemon = totalCollectedPokemon - 1
}
}
holder.bind(pokeDetails)
}
inner class ViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
val pokemonImage = v.findViewById<ImageView>(R.id.pokemon_image)
val pokemonName = v.findViewById<TextView>(R.id.pokemon_name)
val collectedPokemon = v.findViewById<TextView>(R.id.total_collected_pokemon)
val checkbox = v.findViewById<CheckBox>(R.id.checkBox)
fun bind(info:PokeData) {
pokemonName.text = info.pokemonName
val imageRes = v.context.resources.getIdentifier("${info.pokemonImage}", "drawable", v.context.packageName)
pokemonImage.setImageResource(imageRes)
}
}
}
我的数据Class
data class PokeData(val pokemonName : String, val pokemonImage: String) {
}
您可以在 onClick 侦听器中调用 lambda 函数来更新视图中的计数。
适配器构造函数可以像这样更改:
class MainAdapter(val pokeList : List<PokeData>, val onCheckChanged: (selected: Int, total: Int) -> Unit)
并将 onClick 侦听器修改为如下内容:
holder.checkbox.setOnClickListener {
if(holder.checkbox.isChecked) {
totalCollectedPokemon = totalCollectedPokemon + 1
} else {
totalCollectedPokemon = totalCollectedPokemon - 1
}
onCheckChanged(totalCollectedPokemon,list.size)
}
在您的 fragment/activity 中,当您初始化适配器时,您可以处理此 lambda 函数来更新文本视图。
你需要维护一个整数来保存所选卡片(或复选框)的数量。然后实现一个接口,并在单击复选框时将其作为所选卡片的参数计数和列表大小传递。
holder.checkbox.setOnClickListener {
if(holder.checkbox.isChecked) {
totalCollectedPokemon = totalCollectedPokemon + 1
} else {
totalCollectedPokemon = totalCollectedPokemon - 1
}
yourInterface.something(selectedCount,list.size)
}
我正在尝试创建一个简单的图鉴,您可以在其中跟踪收集了多少只宠物小精灵。我遇到的麻烦是 recyclerview 中的复选框。我想让当前为零的总数,选中每个复选框。如果未选中,该数字将下降。总数只是简单地显示在文本视图中。
下图显示了我要解释的内容,以防我没有解释清楚。
这是我的代码
适配器Class
class MainAdapter(val pokeList : List<PokeData>) : RecyclerView.Adapter<MainAdapter.ViewHolder>() {
var totalCollectedPokemon = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater
.inflate(R.layout.poke_contents, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int = pokeList.size
override fun onBindViewHolder(holder: MainAdapter.ViewHolder, position: Int) {
val pokeDetails = pokeList[position]
holder.checkbox.setOnClickListener {
if(holder.checkbox.isChecked) {
totalCollectedPokemon = totalCollectedPokemon + 1
} else {
totalCollectedPokemon = totalCollectedPokemon - 1
}
}
holder.bind(pokeDetails)
}
inner class ViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
val pokemonImage = v.findViewById<ImageView>(R.id.pokemon_image)
val pokemonName = v.findViewById<TextView>(R.id.pokemon_name)
val collectedPokemon = v.findViewById<TextView>(R.id.total_collected_pokemon)
val checkbox = v.findViewById<CheckBox>(R.id.checkBox)
fun bind(info:PokeData) {
pokemonName.text = info.pokemonName
val imageRes = v.context.resources.getIdentifier("${info.pokemonImage}", "drawable", v.context.packageName)
pokemonImage.setImageResource(imageRes)
}
}
}
我的数据Class
data class PokeData(val pokemonName : String, val pokemonImage: String) {
}
您可以在 onClick 侦听器中调用 lambda 函数来更新视图中的计数。
适配器构造函数可以像这样更改:
class MainAdapter(val pokeList : List<PokeData>, val onCheckChanged: (selected: Int, total: Int) -> Unit)
并将 onClick 侦听器修改为如下内容:
holder.checkbox.setOnClickListener {
if(holder.checkbox.isChecked) {
totalCollectedPokemon = totalCollectedPokemon + 1
} else {
totalCollectedPokemon = totalCollectedPokemon - 1
}
onCheckChanged(totalCollectedPokemon,list.size)
}
在您的 fragment/activity 中,当您初始化适配器时,您可以处理此 lambda 函数来更新文本视图。
你需要维护一个整数来保存所选卡片(或复选框)的数量。然后实现一个接口,并在单击复选框时将其作为所选卡片的参数计数和列表大小传递。
holder.checkbox.setOnClickListener {
if(holder.checkbox.isChecked) {
totalCollectedPokemon = totalCollectedPokemon + 1
} else {
totalCollectedPokemon = totalCollectedPokemon - 1
}
yourInterface.something(selectedCount,list.size)
}