mercredi 25 juillet 2018

Check all the check boxes in recycler view [duplicate]

I have a recycler view that gets an array and creates a check box for every item of it. I would like a check box to be on the top of the recycler view which if it's checked, so all the check boxes of recycler view become checked, too. How can I reach that?

Recycler View Adapter:

class RecyclerViewAdapter(val context: Context, val myArray: Array<String>): RecyclerView.Adapter<RecyclerViewAdapter.Holder>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(context).inflate(R.layout.recycler_view_pattern,parent,false)
        return Holder(view)
    }

    override fun getItemCount(): Int {
       return myArray.count()
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
       return holder.bind(myArray[position])
    }

    inner class Holder(itemView: View?): RecyclerView.ViewHolder(itemView){
        val checkBox = itemView?.findViewById<CheckBox>(R.id.checkBox)

        fun bind(str: String){
            checkBox?.text = str

            checkBox?.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
                override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
                    if (checkBox.isChecked){
                       //do something
                    }
                    else{
                        //do something
                    }
                }
            })
        }
    }
}

Recycler View Pattern:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000"
    android:textSize="14sp" />
</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire