In each RecyclerView element, only the first ListView element is displayed.
I’m making an application for passing tests. There may be a different number of answers to each question. There will be an activity in the application that displays a list of all the questions with all the answers, where the correct answer will be marked in color.
Adapter Class:
<code>class QuestionsAdapter: RecyclerView.Adapter<QuestionsAdapter.QuestionHolder>() {
private val questionsList = ArrayList<Question>()
class QuestionHolder(item: View): RecyclerView.ViewHolder(item) {
private val questionBinding = QuestionItemBinding.bind(item)
fun bind(question: Question, position: Int) = with(questionBinding) {
val questionText = "${position + 1}. ${question.text}"
recyclerQuestion.text = questionText
recyclerAnswerList.adapter = AnswersAdapter(this.root.context, R.layout.answer_item_for_questions_list, question.answers)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.question_item, parent, false)
return QuestionHolder(view)
}
override fun getItemCount(): Int {
return questionsList.size
}
override fun onBindViewHolder(holder: QuestionHolder, position: Int) {
holder.bind(questionsList[position], position)
}
fun addQuestion(question: Question) {
questionsList.add(question)
notifyDataSetChanged()
}
fun addAllQuestions(questions: MutableList<Question>) {
questionsList.addAll(questions)
notifyDataSetChanged()
}
}
</code>
<code>class QuestionsAdapter: RecyclerView.Adapter<QuestionsAdapter.QuestionHolder>() {
private val questionsList = ArrayList<Question>()
class QuestionHolder(item: View): RecyclerView.ViewHolder(item) {
private val questionBinding = QuestionItemBinding.bind(item)
fun bind(question: Question, position: Int) = with(questionBinding) {
val questionText = "${position + 1}. ${question.text}"
recyclerQuestion.text = questionText
recyclerAnswerList.adapter = AnswersAdapter(this.root.context, R.layout.answer_item_for_questions_list, question.answers)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.question_item, parent, false)
return QuestionHolder(view)
}
override fun getItemCount(): Int {
return questionsList.size
}
override fun onBindViewHolder(holder: QuestionHolder, position: Int) {
holder.bind(questionsList[position], position)
}
fun addQuestion(question: Question) {
questionsList.add(question)
notifyDataSetChanged()
}
fun addAllQuestions(questions: MutableList<Question>) {
questionsList.addAll(questions)
notifyDataSetChanged()
}
}
</code>
class QuestionsAdapter: RecyclerView.Adapter<QuestionsAdapter.QuestionHolder>() {
private val questionsList = ArrayList<Question>()
class QuestionHolder(item: View): RecyclerView.ViewHolder(item) {
private val questionBinding = QuestionItemBinding.bind(item)
fun bind(question: Question, position: Int) = with(questionBinding) {
val questionText = "${position + 1}. ${question.text}"
recyclerQuestion.text = questionText
recyclerAnswerList.adapter = AnswersAdapter(this.root.context, R.layout.answer_item_for_questions_list, question.answers)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.question_item, parent, false)
return QuestionHolder(view)
}
override fun getItemCount(): Int {
return questionsList.size
}
override fun onBindViewHolder(holder: QuestionHolder, position: Int) {
holder.bind(questionsList[position], position)
}
fun addQuestion(question: Question) {
questionsList.add(question)
notifyDataSetChanged()
}
fun addAllQuestions(questions: MutableList<Question>) {
questionsList.addAll(questions)
notifyDataSetChanged()
}
}
XML File:
<code><androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/recyclerQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />
<ListView
android:id="@+id/recyclerAnswerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:dividerHeight="3dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</code>
<code><androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/recyclerQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />
<ListView
android:id="@+id/recyclerAnswerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:dividerHeight="3dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</code>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/recyclerQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />
<ListView
android:id="@+id/recyclerAnswerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:dividerHeight="3dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>