Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example adapter that uses a ListAdapterWithHeader
package example
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import example.R
import example.ListAdapterWithHeader
class ExampleAdapter : ListAdapterWithHeader<YourDataItem, RecyclerView.ViewHolder>(ITEM_COMPARATOR) {
private var headerItem: HeaderDataItem = HeaderDataItem("Some header data")
// Updates the header item view
fun setHeaderItem(newHeaderItem: HeaderDataItem) {
val previousItem = this.headerItem
this.headerItem = newHeaderItem
if (previousItem != newHeaderItem) {
notifyItemChanged(HEADER_POSITION)
}
}
// Returns the type of view at the given list position
override fun getItemViewType(position: Int): Int {
return if (position == HEADER_POSITION) {
R.layout.item_header_row // header layout not included in example
} else {
R.layout.item_data_row // data item layout not included in example
}
}
// Creates a view holder for the given viewType
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
R.layout.item_header_row -> HeaderItemViewHolder.create(parent)
R.layout.item_data_row -> DataItemViewHolder.create(parent)
else -> throw IllegalArgumentException("Unknown view type $viewType")
}
}
// Binds a view holder to the data at the given list position
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
return when (getItemViewType(position)) {
R.layout.item_header_row -> (holder as HeaderItemViewHolder).bindTo(headerItem)
else -> (holder as DataItemViewHolder).bind(getItem(position))
}
}
companion object {
// The position of the header in the zero-based list
const val HEADER_POSITION = 0
// A DiffUtil.ItemCallback for calculating the diff between two non-null items in a list.
val ITEM_COMPARATOR = object : DiffUtil.ItemCallback<YourDataItem>() {
override fun areContentsTheSame(
oldItem: YourDataItem,
newItem: YourDataItem
): Boolean {
return oldItem == newItem
}
override fun areItemsTheSame(
oldItem: YourDataItem,
newItem: YourDataItem
): Boolean {
return oldItem.itemId == newItem.itemId
}
}
}
}
// A class holding header data
data class HeaderDataItem(
val someHeaderData: String
)
// A view holder for displaying the header row data
class HeaderItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindTo(headerItem: HeaderDataItem) {
// TODO : display the headerItem.someHeaderData
}
companion object {
fun create(parent: ViewGroup): HeaderItemViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_header_row, parent, false)
return HeaderItemViewHolder(view)
}
}
}
// A class that holds the data items in your list
data class YourDataItem(
val itemId: Long,
val someData: String
)
// A view holder for displaying the item row data
class DataItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(dataItem: YourDataItem) {
// TODO : display the dataItem.someData
}
companion object {
fun create(parent: ViewGroup): HeaderItemViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_data_row, parent, false)
return HeaderItemViewHolder(view)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.