How to display total checkbox checked in a recyclerview

醉酒当歌 提交于 2021-01-28 21:52:01

问题


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.

Here is my code

Adapter 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)


        }

    }
}

My Data Class

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

}

回答1:


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)

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)
        }

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




回答2:


you need to maintain an integer that will hold the count of selected card(or checkbox). Then implement an interface and on click checkbox just pass as a param count of selected card and the list size.

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


来源:https://stackoverflow.com/questions/65514968/how-to-display-total-checkbox-checked-in-a-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!