Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

SingleTypeAdapter

SeungHyun edited this page Jun 20, 2020 · 26 revisions

Slush.SingleTypeAdapter is for recycler view with single view type.

Example

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,

  1. Create
  2. Set options
  3. Apply

Create

Slush.SingleTypeAdapter<ITEM>()

ITEM is your item type.

Options

setItemLayout(layoutId) [Required]

Set a layout file id of item.

setLayoutManager(layoutManager) [Required]

Set a layout manager of recycler view.
It is optional if the layout manager is already set.

onBind { view, item -> } or onBindData<ViewDataBinding> { binding, item -> } [Required]

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.

setItems(items)

Set an initial item list.
Parameter items's type is List<ITEM>

onItemClick { clickedView, position -> }

Called when item is clicked.

setDiffCallback(diffCallback)

Set a DiffCallback for recycler view.
DiffCallback is used when you call changeAll method in ItemListEditor.
You can also use BasicDiffCallback.

Apply

into(recyclerView)

Apply to recycler view.

getItemListEditor()

Returns ItemListEditor which allows us to modify the item list.