-
Notifications
You must be signed in to change notification settings - Fork 8
Usage without data binding
Lake edited this page Nov 7, 2021
·
8 revisions
Sometimes, We cannot use data binding for a few reasons (e.g. Complicated view holder, The situation which doesn't use data binding).
For this situation, we support Antonio without data binding.
-
Implement
TypedViewHolder
. (TypedViewHolder implements RecyclerView.ViewHolder)class SmallContentViewHolder(parent: ViewGroup) : TypedViewHolder<ContentSmallModel>( itemView = LayoutInflater.from(parent.context) .inflate(R.layout.view_holder_content_small, parent, false) ) { init { // onCreateView process } override fun onBindViewHolder(data: ContentSmallModel, position: Int, payloads: List<Any>?) { super.onBindViewHolder(data, position, payloads) // onBindViewHolder process } override fun onViewAttachedToWindow(viewHolder: RecyclerView.ViewHolder) { super.onViewAttachedToWindow(viewHolder) } override fun onViewDetachedFromWindow(viewHolder: RecyclerView.ViewHolder) { super.onViewDetachedFromWindow(viewHolder) } override fun onViewRecycled() { super.onViewRecycled() } }
-
Create
AntonioModel
to bind the model on your view holder withMappedWithViewDependency
in Antonio Annotation.@MappedWithViewDependency(viewClass = SmallContentViewHolder::class) data class ContentSmallModel( val id: String, @DrawableRes val iconRes: Int, val price: Int, val onClick: (id: String) -> Unit, val onLongClick: (id: String) -> Boolean, val selectedIds: LiveData<Set<String>> ):AntonioModel
-
Declare
AntonioAdapter
(orAntonioListAdapter
) and Set adapter with your data to RecyclerView.private fun initAdapter(){ // You also can specify the type of Antonio model for the adapter, if you don't need various view types. // e.g., AntonioAdapter<ContentSmallModel>() binding.recyclerView.adapter = AntonioAdapter<AntonioModel>().apply { currentList = viewModel.antonioModels } // Don't forget to set the layout manager to your recycler view :) binding.recyclerView.layoutManager = GridLayoutManager(context,4) viewModel.onDataSetChanged.observe(this) { binding.recyclerView.adapter?.notifyDataSetChanged() } }
-
TypedViewHolder
,PagerViewDependency
,AntonioFragment
are supported.@MappedWithViewDependency( // It can use the class that is implemented by one of the three classes above only. viewClass = SmallContentViewHolder::class)
- TypedViewHolder must have only one viewGroup parameter on their constructor.
- e.g.,
class SmallContentViewHolder( // Require view group only for the constructor's parameter. parent: ViewGroup ) : TypedViewHolder<ContentSmallModel>( itemView = LayoutInflater.from(parent.context) .inflate(R.layout.view_holder_content_small, parent, false) ) { }
- e.g.,
- PagerViewDependency constructor, Fragment constructor must not have any parameters.
- If you need additional parameters for the view dependency constructor, You can link manually without Antonio annotation
Antonio annotation cannot be used when your view holder has some parameters on your constructor.
For this situation, You need to link your model with your view holder builder in the container yourself.
private fun linkAntonio() {
AntonioSettings.viewHolderContainer.add(
ContentSmallModel::class.java,
ViewHolderBuilder { parent ->
return@ViewHolderBuilder SmallContentViewHolder(parent)
}
)
}
private fun linkAntonio() {
val localContainer = ViewHolderContainer().add(ContentSmallModel::class.java,
ViewHolderBuilder { parent ->
return@ViewHolderBuilder SmallContentViewHolder(parent)
}
)
// Don't forget to set the layout manager to your recycler view :)
binding.recyclerView.layoutManager = GridLayoutManager(context,4)
binding.recyclerView.adapter =
AntonioAdapter(viewHolderContainer = localContainer)
}