Switch to jetpack compose. I will archive this project
Adapter that is similar to the ObjectAdapter (ArrayObjectAdapter) from the Leanback widget library (androidx.leanback.widget). Leanbacks package provide many many features and focus on TV. But so many things concludes to complexity.
Focus of this package is to maintain a kotlin lightweight variant. In contrast to leanbacks ObjectAdapter the AnyAdapter is based on RecyclerViews.Adapter.
repositories {
mavenCentral()
}
dependencies {
implementation("de.pfaffenrodt:any-adapter:x.x.x")
}
Please see CHANGELOG for more information on what has changed recently.
What does it do?
- 💥 You do not have to write RecyclerViews.Adapter or, ViewHolder anymore.
- 💄 by implementing a Presenter you can reuse your presentation logic of list items
- databinding support. use DataBindingPresenter and you also do not need write a presenter
- paging support. AnyPagingDataAdapter based on PagingDataAdapter
- ClassPresenterSelector use it to map your domain object to a Presenter
Split presenter logic from adapter logic. Presenter that will create ViewHolder and bind/unbind object to/from ViewHolder
Implement your own Presenter or use DataBindingPresenter. To use multiple viewTypes use PresenterSelector.
val classPresenterSelector = ClassPresenterSelector()
.addClassPresenter(
SampleItemA::class, // item class
SamplePresenterA()
)
val adapter = ArrayObjectAdapter(classPresenterSelector)
val items :List<SampleItemA> = ...
adapter.setItems(items);
What is Data Binding? Doku
Data Binding allows to use ObjectAdapter without implementing an Presenter. DataBindingPresenter will bind and unbind item to/from view.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="item"
type="de.pfaffenrodt.adapter.sample.SampleItem" />
</data>
<TextView
android:id="@+id/text"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{item.text}"
/>
</layout>
DataBindingPresenter(R.layout.item_sample_databinding, BR.item)
.bindVariable(BR.eventHandler, mEventHandler)// bind click listener
Be my Sponsor here on github .