适配器仅显示最后一个项目-Kotlin

时间:2018-09-16 15:44:39

标签: android-recyclerview kotlin adapter recycler-adapter

我有一个在此类别下具有类别和列表的对象。

我的列表应该是这样的:

第1部分 项目1 item2 项目3 组2 项目1 item2

现在要讨论的是,我的列表仅显示带有Item的Section2。

我认为我的适配器出了点问题。

这是适配器:

class FaqAdapter(private val context: Context, private var faqList: List<Faq>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    val view: View
    return when (FaqType.values()[viewType]) {
        FaqType.SECTION -> {
            view = LayoutInflater.from(parent.context).inflate(R.layout.item_section_faq, parent, false)
            SectionViewHolder(view)
        }
        FaqType.QUESTION -> {
            view = LayoutInflater.from(parent.context).inflate(R.layout.item_question_faq, parent, false)
            QuestionViewHolder(view)
        }
    }
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    val faq = faqList[position]
    when (faq.faqType) {
        FaqType.SECTION -> (holder as SectionViewHolder).txtSectionFaq.text = faq.faqCategoryName
        FaqType.QUESTION -> {
            (holder as QuestionViewHolder).txtQuestionFaq.text = faq.question
            holder.txtAnswerFaq.text = faq.answer
        }
    }
}

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

fun update(faqList: List<Faq>) {
    this.faqList = faqList
    notifyDataSetChanged()
}

override fun getItemViewType(position: Int): Int {
    if (true) {
        val faq = faqList[position]

        if (true) {
            return faq.faqType.ordinal
        }
    }
    return 0
}

inner class SectionViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
    @BindView(R.id.txtSectionFaq)
    lateinit var txtSectionFaq: AppCompatTextView

    init {
        ButterKnife.bind(this, itemView)
    }
}

inner class QuestionViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
    @BindView(R.id.txtQuestionFaq)
    lateinit var txtQuestionFaq: AppCompatTextView
    @BindView(R.id.txtAnswerFaq)
    lateinit var txtAnswerFaq: AppCompatTextView

    init {
        ButterKnife.bind(this, itemView)
    }
}

}

0 个答案:

没有答案