如何显示在recyclerview中选中的总计复选框 [英] How to display total checkbox checked in a recyclerview

查看:70
本文介绍了如何显示在recyclerview中选中的总计复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的pokedex,您可以在其中跟踪收集到的宠物小精灵数量.我遇到的麻烦是recyclerview中的复选框.我希望当前总数为零,请选中每个复选框.如果未选中,该数字将下降.总数只是简单地显示在文本视图中.

I am trying to create a simple pokedex where you can keep track of how many pokemons you have collected. The trouble I am having is the checkbox in a recyclerview. I want to have the total which is currently at zero, go up for each checkbox checked. If unchecked the number will go down. The total number is simply just shown in a text view.

下面的图像显示了我要解释的内容,以防万一我没有清楚地解释它.

Below is an image showing what I am trying to explain, in case I am not explaining it as clearly.

这是我的代码

适配器类

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)


        }

    }
}

我的数据类

data class PokeData(val pokemonName : String, val pokemonImage: String) {

}

推荐答案

您可以在onClick侦听器中调用lambda函数,以更新视图中的计数.

You can have a lambda function invoked in onClick listener to update the count in the view.

适配器构造函数可以像这样更改:

Adapter constructor can be changed something like this:

class MainAdapter(val pokeList : List<PokeData>, val onCheckChanged: (selected: Int, total: Int) -> Unit)

并将onClick侦听器修改为以下内容:

and modify onClick listener to something like this:

  holder.checkbox.setOnClickListener {
            if(holder.checkbox.isChecked) {
                totalCollectedPokemon = totalCollectedPokemon + 1
            } else {
                totalCollectedPokemon = totalCollectedPokemon - 1
            }
         
        onCheckChanged(totalCollectedPokemon,list.size)
        }

在片段/活动中,当您初始化适配器时,可以处理此lambda函数以更新文本视图.

In your fragment/activity when you initialize adapter you can handle this lambda function to update the text view.

这篇关于如何显示在recyclerview中选中的总计复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆