3

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>
Javad
  • 147
  • 1
  • 10
  • http://www.zoftino.com/android-recyclerview-checkbox – AskNilesh Jul 25 '18 at 09:51
  • @NileshRathod Dude I know how to create a recycler view with check boxes. I need a "Check All" check box that checks the other check boxes – Javad Jul 25 '18 at 10:00
  • 1
    make a model that wraps the string as well as a boolean to indicate checked or not. Use the boolean to initialize the checkbox in the viewholder bind. When the master checkbox is checked, update all the models to checked=true and update RV adapter with these new models – Tim Jul 25 '18 at 10:04
  • 1
    @TimCastelijns Thanks for the answer but I'm beginner could you write the codes of this solution please? – Javad Jul 25 '18 at 10:09
  • I can but if I do then you learn nothing – Tim Jul 25 '18 at 10:23

2 Answers2

3

Create a global boolean variable in your adapter

public boolean isAllChecked = false;

Then create a public method in your recycler view

public void setAllChecked(boolean isAllChecked) {
    this.isAllChecked = isAllChecked;
    notifyDataSetChanged;
}

Then on your host activity/fragment set a checked changed listener on your "CHECK ALL CHECKBOX" and call the recyclerViewAdapter.setAllChecked() method from inside that mehtod passing in the boolean provided by the onCheckedChangedMethod.

Now edit your bind method based on the condition of isAllChecked

if(isAllChecked){
   // All Check box is checked 
} else {
   // All checkbox is not checked
}
Mohammed Junaid
  • 1,362
  • 14
  • 18
  • Thanks for the help the solution seems alright but it doesn't work I think the problem is that the recycler view doesn't get updated. The whole views are in a dialog. What do you suggest? – Javad Jul 25 '18 at 11:39
  • Did you tried calling 'notifyDataSetChanged()' method from the host i.e from inside the checkedChangedListener of "CheckAll CheckBox" after calling recyclerViewAdapter.setAllChecked() method? – Mohammed Junaid Jul 25 '18 at 12:51
  • Yes I did that too. No Change – Javad Jul 25 '18 at 12:54
  • I tried sth else and it's apparently working. it might seems silly but I created another recycler view adapter just like the previous one with a difference. It checks every check box it makes! And then I set that adapter to my recycler view when the Check All check box is checked. – Javad Jul 25 '18 at 13:00
  • great.. So many possibilities of a workaround in programming.. :D – Mohammed Junaid Jul 25 '18 at 13:02
2

Thanks to everyone who tried to help, I think the only solution for this is to make another recycler view adapter that creates checked check boxes and set it to recycler view whenever the Check All check box is checked.

MainActivity:

checkAllCheckBox.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
           override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
                if (p1){
                      recyclerView.adapter = RecyclerViewAdapter
                }
                else{
                      recyclerView.adapter = AllCheckedRecyclerViewAdapter
                }
            }
})

AllCheckedRecyclerViewAdapter:

inner class AllCheckedRecyclerViewAdapter(val context: Context, val myArray: Array<String>): RecyclerView.Adapter<AllCheckedRecyclerViewAdapter.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.bindIng(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!!.isChecked = true


            ingCheckBox.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
                override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
                    //do sth
                }
            })
        }
    }
}
Javad
  • 147
  • 1
  • 10
  • If you do the .adapter again, the data in the RecyclerView list will return to its initial position, right? – Aldan Sep 24 '20 at 04:11