Search code examples
androidkotlinandroid-recyclerviewradio-buttonadapter

Android RadioButton inside child recyclerView's single selection and uncheck is not working,


I have recyclerview in parentAdapter , which have three different layouts multiple-choice(Title & RadioButtons), Comment (Title & EditText) and Boolean(Title & RadioButtons(Y/N)). These layouts set based on API response. Now boolean and Multichoice have child-recyclerview with seperate child-adapter contains radioButton, expected a single selection but uncheck and single selection is not happening as expected. enter image description here

this is parentAdapter onBindViewHolder,

override fun onBindViewHolder(holder: InspectionAdapter.ViewHolder, position: Int) {
    val model: QueryList = item.get(position)

    if(model.QueryType.equals("YES / NO")){
        holder.booleanQueryTitleLayout.visibility = View.VISIBLE
        holder.booleanQueryBodyLayout.visibility = View.VISIBLE
        holder.stringLayout.visibility = View.GONE
        holder.mcLayout.visibility = View.GONE
        holder.tvBooleanQueryName.text = model.QuestionName
        
        val booleanArray: List<String> = model.QueryType.split("/")
        var booleanList1 : List<String>?=null
        booleanList1 = booleanArray
        booleanList1 = booleanList1.toCollection(ArrayList())

        if(booleanList1.size>0) {
            holder.rvBooleanQuery.layoutManager = LinearLayoutManager(context)
            booleanAdapter = context?.let { BooleanAdapter(it, booleanList1, position, item[position].QueryType) }!!
            holder.rvBooleanQuery.adapter = booleanAdapter
            holder.rvBooleanQuery.setHasFixedSize(true);
            holder.rvBooleanQuery.setLayoutManager(holder.rvBooleanQuery.layoutManager);
            booleanAdapter!!.setOnItemClickListener(this);
        }
    }
    else if(model.QueryType.equals("string")){ //Comment Type
    
    }
    else { // Multi-choice Type
    //multi-choice recyclerView
    }
    
    }

And this is onBindViewHolder() of childAdapter (Boolean case)

override fun onBindViewHolder(holder: BooleanAdapter.ViewHolder, position: Int) {
    Log.e(TAG,"booleanAdapterName"+ item.get(position))
    holder.booleanQueryName.setChecked(position == mSelectedItem)

    holder.booleanQueryName.text = item.get(position)


    if (mSelectedItem == position) {
        holder.booleanQueryName.setChecked(true);
    } else {
        holder.booleanQueryName.setChecked(false);
    }
    holder.booleanRadioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener {
            group, checkedId ->
        this.mSelectedItem = holder.getAdapterPosition()
        if (holder.booleanQueryName.isChecked) {
            mSelectedItem = position
        } else {
            mSelectedItem = -1
        }
    })
    holder.booleanQueryNameYes.setOnClickListener {
       // if(selectedPosition == position){
        hideSoftKeyboard(context, it)
        mSelectedItem = holder.getAdapterPosition()

        if(holder.booleanQueryNameYes.isChecked){
            holder.booleanQueryNameYes.setChecked(true)
            holder.booleanQueryNameYes.isChecked = true
            Toast.makeText(context, "Yes", Toast.LENGTH_SHORT).show()
            notifyDataSetChanged()
        } else {
            holder.booleanQueryNameYes.setChecked(false)
            holder.booleanQueryNameYes.isChecked = false
            Toast.makeText(context, "No", Toast.LENGTH_SHORT).show()
            notifyDataSetChanged()
        }

    }

}

Tried last adapter-pos-tion that selected and tried to do uncheck in if condition,, but that doesnt work. how to achieve this?


Solution

  • Rather than setting any RadioButton to false always use clearCheck() method, that will remove the value of radiobutton and set it to null like the state it was at the creation of radiobutton time.

    This way it will work properly reason behind this is that setting radiobutton to false make it in unchecked state. Unchecked state is exact opposite of checked state but what you need is radio button to be in null state to make it checkable properly once it was checked.