Skip to content

Usage without data binding

Lake edited this page Feb 25, 2023 · 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.

With AntonioAnnotation.

  • Add dependencies in your app level build.gradle.

    dependencies {
        def antonioAnnotationVersion = '0.0.6-alpha'
        implementation "io.github.naverz:antonio-annotation:$antonioAnnotationVersion"
        //For java (You must select only one of kapt, ksp and annotationProcessor)
        annotationProcessor "io.github.naverz:antonio-compiler:$antonioAnnotationVersion"
        //For kotlin kapt (You must select only one of kapt, ksp and annotationProcessor)
        kapt "io.github.naverz:antonio-compiler:$antonioAnnotationVersion"
        //For kotlin ksp (You must select only one of kapt, ksp and annotationProcessor)
        ksp "io.github.naverz:antonio-compiler:$antonioAnnotationVersion"
    }
  • 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 AntonioModel to bind the model on your view holder with MappedWithViewDependency 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 (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()
          }
     }
  • After the first build, Put init code for the annotation on Application's onCreate or Launch activity's onCreate.

    fun initialize(){
         AntonioAnnotation.init()
    }

Preconditions for the annotation

  • 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)
          ) {
      }
  • 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

Without Antonio annotation.

  • 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)
     }