This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@BindingAdapter("items") | |
fun setRecyclerViewItems( | |
recyclerView: RecyclerView, | |
items: List<RecyclerItem>? | |
) { | |
var adapter = (recyclerView.adapter as? RecyclerViewAdapter) | |
if (adapter == null) { | |
adapter = RecyclerViewAdapter() | |
recyclerView.adapter = adapter | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class RecyclerItem( | |
val data: Any, | |
@LayoutRes val layoutId: Int, | |
val variableId: Int | |
) { | |
fun bind(binding: ViewDataBinding) { | |
binding.setVariable(variableId, data) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RecyclerViewAdapter : RecyclerView.Adapter<BindingViewHolder>() { | |
private val items = mutableListOf<RecyclerItem>() | |
override fun getItemCount(): Int { | |
return items.size | |
} | |
override fun getItemViewType(position: Int): Int { | |
return getItem(position).layoutId |