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

时间:2020-04-07 08:09:18

标签: 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
        }

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

1 个答案:

答案 0 :(得分:0)

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

遵循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)
     }
}