Skip to content

Plug in

Lake edited this page Nov 13, 2021 · 4 revisions

With data binding

It's similar to the Basic usage for recycler view. Just change the adapter for view pager and the paging library!

ViewPager

  • Use AntonioPagerAdapter!
   private fun initAdapter(){
        binding.recyclerView.adapter =
            AntonioPagerAdapter<AntonioModel>().apply { 
                currentList = viewModel.antonioModels
                // If you are gonna setup `TabLayout`, You should set titles.
                titles = viewModel.titles
            }
        viewModel.onDataSetChanged.observe(this) {
            binding.recyclerView.adapter?.notifyDataSetChanged()
        }
   }

ViewPager2

  • Use AntonioAdapter or AntonioListAdapter
   private fun initAdapter(){
        binding.viewPager2.adapter =
            AntonioAdapter<AntonioModel>().apply { currentList = viewModel.antonioModels }
        // or You can use AntonioListAdapter when you want to use DiffUtil!
        viewModel.onDataSetChanged.observe(this) {
            binding.recyclerView.adapter?.notifyDataSetChanged()
        }
   }
  • AntonioFragmentStateAdapter
   // If you want to save your state when it's on system-initiated process death, Implement parcelable for your antonio model. 
   @Parcelize
   data class SomeAntonioParcelableModel() : AntonioBindingModel, Parcelable {
       override fun layoutId(): Int = R.layout.fragment_antonio
       override fun bindingVariableId(): Int = BR.model
   }

   // If you don't need to handle system-initiated process death, You just implement normal AntonioBindingModel.
   data class SomeAntonioBindingModel() : AntonioBindingModel, Parcelable {
       override fun layoutId(): Int = R.layout.fragment_antonio
       override fun bindingVariableId(): Int = BR.model
   }

   private fun initAdapter(){
        binding.viewPager2.adapter =
            AntonioFragmentStateAdapter<AntonioModel>().apply { currentList = viewModel.antonioModels }
        viewModel.onDataSetChanged.observe(this) {
            binding.recyclerView.adapter?.notifyDataSetChanged()
        }
   }

Paging2 / Paging3

  • AntonioPagingDataAdapter (Sample)
  // ViewModel with PagingSource
  class PagingFragmentViewModel : ViewModel() {
      val flow = Pager(
          PagingConfig(pageSize = 20)
      ) { SamplePagingSource() }.flow.cachedIn(viewModelScope)
  }

  // Usage
  private val viewModel by viewModels<PagingFragmentViewModel>()

  private fun initAdapter(){
        val adapter = AntonioPagingDataAdapter<AntonioModel>(someHashCallback)
        binding.recyclerView.adapter = adapter
        viewLifecycleOwner.lifecycleScope.launch {
            viewModel.flow.collectLatest(adapter::submitData)
        }
   }
  • AntonioPagedListAdapter
    fun initAdapter() {
       val pagedList: PagedList<AntonioModel> = viewModel.pagedList
       val adapter = AntonioPagedListAdapter(HashDiffItemCallback()).submitList(pagedList)
       binding?.recyclerView?.adapter = adapter
    }

Without data binding

To use plug-in without data binding, You should know the basic usage without data-binding first. Please, read the link first!

For plug-in without data-binding, You just can change your view dependencies.

View dependencies

There are each view dependencies for plug-ins. You can implement them!
All dependencies are here.

ViewPager

PagerViewDependency

interface PagerViewDependency<ITEM : AntonioModel> : GenericAntonioFindable {
    fun getView(container: ViewGroup, position: Int, viewType: Int, antonioModel: ITEM): View
    fun instantiateItem(container: ViewGroup, position: Int, viewType: Int, antonioModel: ITEM): Any
    fun destroyItem(container: ViewGroup, position: Int, antonioModel: ITEM, any: Any)
    fun getPageWidth(position: Int, antonioModel: ITEM): Float
}

AntonioPagerView

abstract class AntonioPagerView<ITEM : AntonioModel> {
    /**
     * Fragment which has this view.
     * It can be null if it's not in fragment or it's not instantiated.
     */
    protected var fragment: Fragment? = null
   

    /**
     * Activity which has this view.
     * It can be null, if the activity which has this view doesn't inherit [ComponentActivity] or it's not instantiated.
     */
    protected var activity: ComponentActivity? = null
        
    override fun getView(container: ViewGroup, position: Int, viewType: Int, antonioModel: ITEM): View
    override fun onViewCreated(view: View, position: Int, antonioModel: ITEM) 
    override fun onDestroyedView(position: Int, antonioModel: ITEM)
}

ViewPager2

ViewHolder

Same with the basic usage without data-binding!

Fragment

Implement AntonioFragment