Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Los botones de opción dentro de RecyclerView siguen estando desmarcados aleatoriamente incluso cuando configuro explícitamente el estado marcado en un botón de opción

Para hacer que ViewHolder trate cada botón de opción por separado y no reutilice el estado del botón de opción anterior, almacené el estado de si un elemento de botón de opción está marcado o no en una lista de valores booleanos y configuré el estado de los botones de opción de acuerdo con esa lista. pero cuando me desplazo hacia abajo y hacia arriba, los botones de opción alternados se deshabilitan aleatoriamente a pesar de que isCheckedList contiene la información booleana correcta para cada botón de opción (imprimo la lista con logd() para verificar si realmente almacena los estados)

¿Alguna idea de lo que me estoy perdiendo?

 class QuestionAdapter(context: Context) : RecyclerView.Adapter<QuestionAdapter.QuestionViewHolder>() { private val listOfQuestion: List<String> private val listSize = 20 private val isCheckedList: MutableList<Boolean> = MutableList(listSize) { false } //Initialize a list of 20 questions. init { val allQuestions = context.resources.getStringArray(R.array.questions).toList() listOfQuestion = allQuestions .shuffled() .take(listSize) } class QuestionViewHolder(val view: View) : RecyclerView.ViewHolder(view) { val questionText = view.findViewById<TextView>(R.id.question) val radioButton = view.findViewById<RadioButton>(R.id.yes) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionViewHolder { val layout = LayoutInflater .from(parent.context) .inflate(R.layout.item_view, parent, false) return QuestionViewHolder(layout) } override fun onBindViewHolder(holder: QuestionViewHolder, position: Int) { val item = listOfQuestion[position] holder.questionText.text = item holder.radioButton.isChecked = isCheckedList[position] holder.radioButton.setOnCheckedChangeListener { _, isChecked -> isCheckedList[holder.adapterPosition] = isChecked Log.d("Adapter", "list: $isCheckedList ") } } override fun getItemCount(): Int { return listOfQuestion.size } }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Resolví el problema agregando esta línea de código holder.radioGroup.clearCheck() antes de configurar el estado de los botones de opción. Para más detalles, este es un blog que habla sobre el mismo problema y cómo lo resolvió.

over 4 years ago · Santiago Trujillo Report

0

Debe borrar la selección en el botón de radio antes de establecer su valor correcto.

 holder.radioGroup.clearCheck()

De esta manera, el grupo de RadioGroup borra su selección, listo para recibir una entrada que activará el botón de radio correcto RadioButton lo marcado.

Pero debe tener en cuenta que esto podría llamar a sus oyentes onCheckedChanged y podría generar resultados inesperados. Pero hay una solución para el problema resultante.

 // Nullify listener holder.radioGroup.setOnCheckedChangeListener(null); // Clear selection holder.radioGroup.clearCheck(); // Reset listener after selection has been cleared holder.radioGroup.setOnCheckedChangeListener(checkedChangeListener);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!