Android Kotlin:Groupie RecyclerView添加到同一可扩展项目的所有项目



首先,我的问题与此相同。但我的代码不同,我试图遵循这一点,但没有解决我的问题。

所以我的问题是,我想要这个:

  • 标题1
    • 子标题1
    • 子标题1
    • 子标题1
  • 收割台2
    • 子标题2
    • 子标题2

我有这个:

  • 标题1
    • 子标题1
    • 子标题1
    • 子标题1
    • 子标题2
    • 子标题2
  • 收割台2
    • 子标题1
    • 子标题1
    • 子标题1
    • 子标题2
    • 子标题2

这是我的ParentItemAdapter代码

class ParentItemAdapter(private val titlePlan: String?, private val fragment: FragmentActivity) :
Item(), ExpandableItem {
private lateinit var expandableGroup: ExpandableGroup
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.root.tvTitlePlan.text =
String.format(fragment.getString(R.string.plan), titlePlan)
viewHolder.itemView.setOnClickListener {
expandableGroup.onToggleExpanded()
changeStuff(viewHolder)
}
}
override fun getLayout(): Int {
return R.layout.item_benefit_corporate_parent
}
private fun changeStuff(viewHolder: GroupieViewHolder) {
viewHolder.root.indicator.apply {
setImageResource(
if (expandableGroup.isExpanded) R.drawable.ic_arrow_up_red
else R.drawable.ic_arrow_down_red
)
}
}
override fun setExpandableGroup(onToggleListener: ExpandableGroup) {
this.expandableGroup = onToggleListener
}
}

这是我的ChildItemAdapter代码

class ChildItemAdapter(
private val guarantee: String?,
private val statusBenefit: String?,
private val benefitLimit: Long?
) : Item() {
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.root.tvTitleGuaranteeChild.text = guarantee
viewHolder.root.tvStatusChild.text = statusBenefit
viewHolder.root.tvBenefitLimitChild.text = benefitLimit?.applyThousandSeparator()
}
override fun getLayout(): Int {
return R.layout.item_benefit_corporate_child
}
}

这是我的碎片。

class CorporateBenefitFragment :
BaseFragment<FragmentCorporateBenefitBinding, CorporateBenefitViewModel>() {
private val groupAdapter = GroupAdapter<GroupieViewHolder>()
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
initRecyclerView()
vm.getBenefitCorporate(regSpaj, msteInsured)
vm.benefit.observe(viewLifecycleOwner, {
it?.let {
when (it) {
...
is ResultState.HasData -> {
...
mappingData(it.data)
}
...
}
}
})
}
private fun initRecyclerView() {
val layoutManager = LinearLayoutManager(activity)
val divider =
DividerItemDecoration(binding.expandableBenefit.context, layoutManager.orientation)
binding.expandableBenefit.layoutManager = layoutManager
binding.expandableBenefit.adapter = groupAdapter
binding.expandableBenefit.addItemDecoration(divider)
}
private fun mappingData(data: List<Benefit>) {
val parentList = mutableListOf<ParentItemAdapter>()
val childList = mutableListOf<ChildItemAdapter>()
for (i in data) {
parentList.add(ParentItemAdapter(i.namePlan, requireActivity()))
for (a in i.detailBenefit) {
childList.add(ChildItemAdapter(a.kindOfGuarantee, a.benefitStatus, a.benefitLimit))
}
}
groupAdapter.apply {
for (i in parentList) {
this += ExpandableGroup(i).apply {
for (j in childList) { // I think the problem was here, but I don't know how to fix it
add(j)
}
}
}
}
}
...
}

非常感谢。

我用这段代码解决了我的问题。

private fun mappingData(data: List<Benefit>) {
val parentList = mutableListOf<ParentItemAdapter>()
for (i in data) {
parentList.add(ParentItemAdapter(i.namePlan, requireActivity()))
}
groupAdapter.apply {
for (i in 0 until parentList.size) {
this += ExpandableGroup(parentList[i]).apply {
for (j in data[i].detailBenefit) {
add(ChildItemAdapter(j.kindOfGuarantee, j.benefitStatus, j.benefitLimit))
}
}
}
}
}

或者用这个

private fun mappingData(data: List<Benefit>) {
for (i in data) {
val parentItem = ParentItemAdapter(i.namePlan, requireActivity())
val expandableGroup = ExpandableGroup(parentItem, false)
for (j in i.detailBenefit) {
val childItem = ChildItemAdapter(j.kindOfGuarantee, j.benefitStatus, j.benefitLimit)
expandableGroup.add(Section(childItem))
}
groupAdapter.add(expandableGroup)
}
}

最新更新