-
Notifications
You must be signed in to change notification settings - Fork 4
SingleTypeAdapter
Slush.SingleTypeAdapter
is for recycler view with single view type.
val listEditor = Slush.SingleTypeAdapter<Book>()
.setItemLayout(R.layout.item_book)
.setItems(items)
.setLayoutManager(LinearLayoutManager(this))
.onBind { view, book ->
view.bookName.text = book.name
}
.onItemClick { clickedView, position ->
Log.d(TAG, "Clicked: $position")
}
.setDiffCallback(BasicDiffCallback())
.into(recyclerView)
.itemListEditor
listEditor.addItem(Book("New Book"))
SingleTypeAdapter
follow three steps below,
- Create
- Set options
- Apply
ITEM
is your item type.
Set a layout file id of item.
Set a layout manager of recycler view.
It is optional if the layout manager is already set.
Similar to RecyclerView.Adapter's onBindViewHolder.
onBind
view
is the item view.
item
's type is ITEM
.
onBindData
In kotlin, binding
's type is your generated binding class passed by generic.
In java, binding
's type is ViewDataBinding
so that you need to cast it to your binding class.
item
's type is ITEM
.
Set an initial item list.
Parameter items's type is List<ITEM>
Called when item is clicked.
Set a DiffCallback for recycler view.
DiffCallback is used when you call changeAll
method in ItemListEditor
.
You can also use BasicDiffCallback
.
Apply to recycler view.
Returns ItemListEditor which allows us to modify the item list.