- Step1 : create a Model
- Step2 : create a layout file for component(A ViewHolder)
- Step3 : create a AdapterDelegate and register it.
1)Create a Model, lets say SimpleTextModel
:
data class SimpleTextModel(val content: String)
2)A simple layout that containers a TextView :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0ff0ff"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="50dp"
android:paddingLeft="16dp"
android:text="content"
android:textColor="@android:color/white"
android:textSize="16sp" />
</FrameLayout>
3)Create a AdapterDelegate
by adapterDelegate
DSL ,and
override onBind
method:
val simpleTextDelegate = adapterDelegate<SimpleTextModel>(R.layout.flap_item_simple_text) {
onBind { model ->
bindTextView(R.id.tv_content) {
text = model.content
}
}
}
Use a FlapAdapter
instead of Adapter
and register AdapterDelegate to a FlapAdapter: :
//create FlapAdapter
var adapter: FlapAdapter = FlapAdapter()
//register AdapterDelegate
adapter.registerAdapterDelegate(simpleTextDelegate)
val dataList = ArrayList<Any>()
dataList.add(SimpleTextModel("Android"))
dataList.add(SimpleTextModel("Java"))
dataList.add(SimpleTextModel("Kotlin"))
//setData
adapter.setDataAndNotify(dataList);
recyclerView.adapter = adapter
Here we go!!
Apache 2.0