Skip to content

Usage without data binding

Lake edited this page Sep 1, 2022 · 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 (You can change this part to another view dependencies for ViewPager, ViewPager2).

    // 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 the model which is implemented AntonioModel to bind the model on your view holder.

    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
  • Link your view holder with the model class with the global container

    AntonioSettings.viewHolderContainer.add(
       ContentSmallModel::class.java,
       ViewHolderBuilder { parent ->
           return@ViewHolderBuilder SmallContentViewHolder(parent)
       }
    )
  • Declare AntonioAdapter (or AntonioListAdapter) 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()
          }
     }
  • (Optional) If you don't want to share your view holder globally, You can also use a local container when you create your Adapter.

     private fun linkAntonio() {
         val localContainer = ViewHolderContainer().add(ContentSmallModel::class.java,
             ViewHolderBuilder { parent ->
                 return@ViewHolderBuilder SmallContentViewHolder(parent)
             }
         )
         binding.recyclerView.layoutManager = GridLayoutManager(context,4)
         binding.recyclerView.adapter =
             AntonioAdapter(viewHolderContainer = localContainer)
     }

Clone this wiki locally