android 需要帮助正确处理recyclerview的状态

pftdvrlh  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(126)

我有一个很难弄清楚如何让recyclerview工作,我希望它的工作。到目前为止,我所理解的是,在一个房车项目被回收后,如果你没有正确处理状态,它可能会导致一些意外。所以我有一个recyclerview适配器,它显示一个问题项。基本上是问题本身的文本视图和答案的5个复选框的组合。
我正在改变答案文本的颜色,绿色或红色取决于它是对还是错。我面临的问题是,当视图回收时,它会返回到默认状态。(未选中的复选框和未着色的答案)我想有它,以便改变状态后,揭示答案停留。看看我的适配器类

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jaytech.qcmvault.R
import com.jaytech.qcmvault.data.Question

class QuestionAdapter : RecyclerView.Adapter<QuestionAdapter.QuestionViewHolder>() {
    var allQuestions = ArrayList<Question>()

    inner class QuestionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val question:TextView = itemView.findViewById(R.id.tv_question)
        val answerA:CheckBox = itemView.findViewById(R.id.answerA)
        val answerB:CheckBox = itemView.findViewById(R.id.answerB)
        val answerC:CheckBox = itemView.findViewById(R.id.answerC)
        val answerD:CheckBox = itemView.findViewById(R.id.answerD)
        val answerE:CheckBox = itemView.findViewById(R.id.answerE)

        val optionError: TextView = itemView.findViewById(R.id.optionError)
        val correctButton:Button = itemView.findViewById(R.id.correctButton)
        val retryButton:Button = itemView.findViewById(R.id.retryButton)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.question_item, parent, false)
        return QuestionViewHolder(view)
    }

    override fun getItemCount(): Int {
        return allQuestions.size
    }

    override fun onBindViewHolder(holder: QuestionViewHolder, position: Int) {
        holder.apply {
            optionError.visibility = View.GONE

            question.text = allQuestions[position].question
            answerA.text = allQuestions[position].answers[0]
            answerA.setTextColor(Color.BLACK)
            answerA.isChecked = false

            answerB.text = allQuestions[position].answers[1]
            answerB.setTextColor(Color.BLACK)
            answerB.isChecked = false

            answerC.text = allQuestions[position].answers[2]
            answerC.setTextColor(Color.BLACK)
            answerC.isChecked = false

            answerD.text = allQuestions[position].answers[3]
            answerD.setTextColor(Color.BLACK)
            answerD.isChecked = false

            answerE.text = allQuestions[position].answers[4]
            answerE.setTextColor(Color.BLACK)
            answerE.isChecked = false

            val correctAnswers: List<Boolean> = allQuestions[position].correctAnswers
            val answersCheckboxList = arrayOf(answerA, answerB, answerC, answerD, answerE)

            correctButton.setOnClickListener {
                for (i in answersCheckboxList.indices) {
                    val checkBox = answersCheckboxList[i]
                    val isCorrect = correctAnswers[i]

                    if (answerA.isChecked || answerB.isChecked || answerC.isChecked || answerC.isChecked || answerD.isChecked || answerE.isChecked) {
                        if (isCorrect) {
                            checkBox.setTextColor(Color.GREEN)
                            optionError.visibility = View.GONE

                        } else checkBox.setTextColor(Color.RED)
                    } else optionError.visibility = View.VISIBLE
                }
            }

            retryButton.setOnClickListener {
                answersCheckboxList.forEach {
                    it.setTextColor(Color.BLACK)
                    it.isChecked = false
                }
                optionError.visibility = View.GONE

            }

        }
    }
}

字符串
这也是我问题课

data class Question(
    val question:String?,
    val answers:List<String>,
    val correctAnswers:List<Boolean>
)


我试着像这样手动处理状态

answerA.setTextColor(Color.BLACK)
            answerA.isChecked = false

  answerB.setTextColor(Color.BLACK)
            answerB.isChecked = false

  answerC.setTextColor(Color.BLACK)
            answerC.isChecked = false

  answerD.setTextColor(Color.BLACK)
            answerD.isChecked = false

  answerE.setTextColor(Color.BLACK)
            answerE.isChecked = false


我希望它能防止下面的视图出现不必要的更改状态。它工作,但最终发生的是它重置了我与之交互的视图项,这不是我想要的。我想让它离开那些与我互动的人,只对那些我还没有采取行动

smdnsysy

smdnsysy1#

您可以使用Question数据类本身来存储所选答案。
1.创建一个整数字段,用于存储所选答案

var selectedAnswer: Int

1.使用状态更改侦听器保存所选答案。为了存储使用,可以使用分别对应于answerA、answerB、answerC和answerD复选框的1、2、3和4。你也可以根据自己的选择使用String或enum来Map它。

// do this for all check boxes with different values to selected answer
answerA.setOnCheckedChangeListener(
             new CompoundButton.OnCheckedChangeListener() {
                 @Override
                 public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                    if(isChecked)
                     allQuestions[position].selectedAnswer = 1
                 }
             }
     );


1.获取onBindViewHolder中选定答案的值,并相应地管理复选框的状态

when(allQuestions[position].selectedAnswer){
      1 -> answerA.setChecked = true
      2 -> answerB.setChecked = true
      3 -> answerC.setChecked = true
      4 -> answerD.setChecked = true
 }

如果你有多个正确的选项,问题类型,你可以使用一个数组来存储选择的答案,而不仅仅是一个int变量。

相关问题