/slush

No more boilerplate adapters and view holders. Slush will make using RecyclerView easy and fast.

Primary LanguageKotlinMIT LicenseMIT


한국어

Download Android CI

No more boilerplate adapters and view holders.
Slush will make using RecyclerView easy and fast.

The goal of this project is to make RecyclerView, which is not very complicated, possible with Slush.

Features

  • ItemClickListener
  • DataBinding
  • DiffCallback
  • ObservableArrayList
  • LiveData
  • MultiType Adapter
  • (Divider)ItemDecoration
  • Swipe option

Setup

Add a dependency below at your build.gradle(app)

dependencies {
    implementation 'in.seunghyun:slush:1.2.2'
}

Basic Usage

You can use all the features with a single class called Slush.

Basically, you have to write some codes below. (Here, Replace the SimpleItem part into your item class to use.)

Slush.SingleType<SimpleItem>()
    .setItemLayout(R.layout.simple_binding_item) // Set item layout
    .setLayoutManager(LinearLayoutManager(this)) // Set LayoutManager (No need to write if already exists)
    .onBind { view, item ->
        view.itemName.text = item.name
    }
    .into(recyclerView)

Initialize views in item layout in onBind. (Similar to onBindViewHolder of RecyclerView Adapter)

ItemListEditor

ItemListEditor is an interface that allows you to edit the item list.

val result = Slush.SingleType<SimpleItem>()
    // Omit intermediate code
    .into(recyclerView)
val itemListEditor = result.itemListEditor

The available methods are listed here.

DataBinding

Slush supports data binding.

You should change onBind part to onBindData to use data binding.

.onBindData<SimpleItemBinding> { binding, item ->
    binding.item = item
}

And put your generated binding class into SimpleItemBinding part.

LiveData, ObservableList

If you use LiveData or ObservableList, you don't need to use ItemListEditor because slush will automatically observe the list and update RecyclerView.

LiveData

val itemsLiveData = MutableLiveData<List<SimpleItem>>()

Slush.SingleType<SimpleItem>()
    // Omit intermediate code
    .setItems(itemsLiveData, lifecycleOwner)
    .into(recyclerView)

Full code

Furthermore

BasicDiffCallback is applied by default when using LiveData.
If you don't want to use DiffCallback, you can put false as the third argument of setItems

.setItems(itemsLiveData, lifecycleOwner, false)

ObservableList

val observableItems = ObservableArrayList<SimpleItem>()

Slush.SingleType<SimpleItem>()
    // Omit intermediate code
    .setItems(observableItems)
    .into(recyclerView)

Full code

Furthermore

Slush does not remove the observer automatically.
So you can use ObserveController to remove the observer.

val result = Slush.SingleType<SimpleItem>()
    // Omit intermediate code
    .setItems(observableItems)
    .into(recyclerView)

val observeController = ObserveController(result)
observeController.stopObserving()

Options

You can apply these options selectively.

setItems

.setItems(items)

Set an initial item list.

onItemClick

.onItemClick { clickedView, position ->
    Log.d(TAG, "Clicked: $position")
}

Set a listener to be executed when item is clicked.

setDiffCallback

.setDiffCallback(BasicDiffCallback())

Set a DiffCallback to be used when you call changeAll in ItemListEditor
By default, calling changeAll replaces all data in the list, but if you set DiffCallback, only the modified part is changed, so animation occurs.
You can also use custom DiffCallback by inheriting SlushDiffCallback.

Samples

You can also see the sample codes here.

License

MIT License

Copyright (c) 2020 SeungHyun

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.