android - 如何在Android中使用Groupie在Recyclerview中实现HeaderItems

标签 android kotlin android-recyclerview

我正在尝试使用Groupie与HeaderItems创建一个recyclerview。我有这样的数据组

class Group(
    val id: String = generateId(),
    val name: String? = null,
    val entries: List<Entry>? = null
) : Item(), Parcelable {
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        viewHolder.apply {
            itemView.tvGroupName.text = name
        }
    }

    override fun getLayout() = R.layout.group_single_item

    constructor(source: Parcel) : this(
        source.readString(),
        source.readString(),
        source.createTypedArrayList(Entry.CREATOR)
    )


    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeString(id)
        writeString(name)
        writeTypedList(entries)
    }

    companion object {
        private fun generateId(): String {
            return UUID.randomUUID().toString()
        }

        @JvmField
        val CREATOR: Parcelable.Creator<Group> = object : Parcelable.Creator<Group> {
            override fun createFromParcel(source: Parcel): Group = Group(source)
            override fun newArray(size: Int): Array<Group?> = arrayOfNulls(size)
        }
    }


}

每个组都有一个条目列表
data class Entry(val id: Long=0, val name: String) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readLong(),
        parcel.readString()
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeLong(id)
        parcel.writeString(name)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Entry> {
        override fun createFromParcel(parcel: Parcel): Entry {
            return Entry(parcel)
        }

        override fun newArray(size: Int): Array<Entry?> {
            return arrayOfNulls(size)
        }
    }
}

因此,我试图显示一个组列表以及它们各自的条目。因此,我将显示一个带有名称和条目列表的组。所以我想到了使用Groupie。

这就是我一直在尝试的
val linearLayoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        val groups = intent.getParcelableArrayListExtra<Group>("groups")


        val groupAdapter = GroupAdapter<GroupieViewHolder>().apply {

            val section = Section(Group())
            section.setHeader(Group())
            section.addAll(groups)
            this.add(section)

        }


        recyclerViewGroups.apply {
            layoutManager = linearLayoutManager
            adapter = groupAdapter
        }

但是我不太确定如何添加组及其条目。任何帮助,将不胜感激。谢谢

最佳答案

首先,您需要为您的组创建item类(可能是headerentry)。

请遵循this section中的指示。

例如。这些可能是:

class HeaderItem(private val groupName: String) : Item() {

    //... to be implemented
}


class EntryItem(private val entryName: String) : Item() {

    //... to be implemented
}

然后在适配器中使用它们(需要进行测试,我将此写在头上):
val groupAdapter = GroupAdapter<GroupieViewHolder>().apply {
    groups.forEach { group ->
        val section = Section()
        section.setHeader(HeaderItem(group.name))
        section.addAll(group.entries.map{ it -> EntryItem(it.name) })
        this.add(section)
     }
}

关于android - 如何在Android中使用Groupie在Recyclerview中实现HeaderItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61075506/

相关文章:

java - LocationListener 未获取用户的当前位置

android - 抛出异常并在 "error"级别的 logcat 中打印其堆栈跟踪

android - 如何处理 RecyclerView.ItemDecoration 中的点击事件?

android - Firebase Android 应用程序 - 不更新实时数据库

android - 无法让多点触控在 Unity3D 中工作

android - RxAndroidBle WRITE_TYPE_DEFAULT 或 WRITE_TYPE_NO_RESPONSE

android - 为什么我可以将 null 参数传递给 Kotlin 中需要非 null 的 fun ?

android-studio - 如何在Kotlin Android中为屁股添加+/-功能

android - RecyclerView 项目 wrap_content 宽度

android - 如何在 Android RecyclerView 和 LayoutManager 中使用 scrollVerticallyBy()?