RecyclerView.Adapter

Current version number:

  • Auxiliary Android developers quickly create RecyclerView.Adapter implementation.
  • Extremely friendly for projects where ViewBinding and DataBinding have been introduced and requires only 2-5 lines of code.
  • Various adapters for DataBinding, ViewBinding with plain lists, and Paging3 are available for a variety of needs.

Implement the Identifiable interface on the data class

// The data class, which implements the Identifiable
      
        interface and an access to Identity
      
// Identity can be any type and is specified in the generic argument
data class Item(val id: Int.val name: String): Identifiable<Int> {
    override val identity: Int 
        get() = id
}
Copy the code

DataBinding

  • If you use DataBinding, you can simply declare one Adapter to complete the creation.
// Declare an Adapter inherited from AutomaticBindingAdapter
// We need to declare a variable of the specified type in the XML corresponding to ItemBinding:
// <variable name="item" type="Item" />
// <variable name="onClickListener" type="View.OnClickListener" />
// And bind the item and onClickListener to the corresponding View. View. onClickListener is optional
class ItemAdapter : AutomaticBindingAdapter<Item, ItemBinding>(a)// Then start using
val itemAdapter = ItemAdapter()
recyclerView.adapter = itemAdapter
// Submit data to Adapter
itemListViewModel.itemListLiveData.observe(this) { list ->
    itemAdapter.submitList(list)    
}
// Listen for click events
itemAdapter.clickEvent.observe(this) { (view: View, item: Item) ->
    //do something...
}
// Or only care about data type click events
itemAdapter.clickItemEvent.observe(this) { item: Item ->
    //do something...
}
Copy the code

ViewBinding

  • MutableBindingAdapter + ViewBindingHolder is used when the AutomaticBindingAdapter cannot meet the requirements
// Create an Adapter that inherits from MutableBindingAdapter and takes three generic parameters:
// AD -> Implement the Identifiable data class in the Identifiable interface
// VH -> ViewBindingHolder
// VB -> create a ViewBinding or ViewDataBinding implementation class
// We need to pass a factory method to the constructor that creates VH from VB. Usually, we just refer to the constructor of the VH implementation class directly
// If this argument is not passed, then the onCreateViewHolder method needs to be implemented
class ItemAdapter : MutableBindingAdapter<Item, ItemBinding, ItemHolder>(::ItemHolder)

// Declare a reuse ViewHolder that takes two generic arguments, which are:
// AD -> Implement the Identifiable data class in the Identifiable interface
// VB -> Item layout automatically generated ViewBinding implementation class
// Implement the binding method to meet various requirements.
class ItemHolder(binding: ItemBinding) : ViewBindingHolder<Item, ItemBinding>(binding) {

    init {
        binding.root.setOnClickListener {
            val item = list.getItem(layoutPosition)
            //do something...}}override fun binding(position: Int) {
        // Click events are provided by default, and can be obtained by listening to clickEvent:
        //binding.root.setOnClickListener(clickEvent)
        binding.textView.text = list.getItem(position).toString()
    }
}

/ / use
val itemAdapter = ItemAdapter()
recyclerView.adapter = itemAdapter
// Submit data to Adapter
itemListViewModel.itemListLiveData.observe(this) { list ->
    itemAdapter.submitList(list)    
}
Copy the code

List table header and table tail placeholder

  • Occasionally encounter the need to start or end the RecyclerView in the position to show some fixed content of the demand, this Adapter getItemCount and List of size is not equal, when the List of data changes, there will be a RecyclerView animation is not normal, Incorrect loading and other problems, here can also provide solutions:
/ / MutableBindingAdapter, AutomaticBindingAdapter:
class ItemAdapter : AutomaticBindingAdapter<NumberItem, NumberViewBinding>() {

    init {
        // How many header and tail entries need to be displayed
        headerCount = 2
        trailCount = 3
    }

    override fun onCreateHeaderViewHolder(parent: ViewGroup): ViewBindingHolder<out Identifiable<*>, ViewBinding> {
        // Create a ViewHolder for the header
        return HeaderHolder(HeaderBinding.inflate(LayoutInflater.from(parent.context), parent, false))}override fun onCreateTrailViewHolder(parent: ViewGroup): ViewBindingHolder<out Identifiable<*>, ViewBinding> {
        // Create a ViewHolder for the end of the table
        return TrailHolder(TrailBinding.inflate(LayoutInflater.from(parent.context), parent, false))}}class HeaderHolder(binding: HeaderViewBinding): ViewBindingHolder<out Identifiable<*>, HeaderViewBinding>(binding) {
    override fun binding(position: Int) {
        // Position has corrected the true position of this ViewHolder in the table header
        // The list: ListHelper tool cannot be used
        // Bind the view to the data}}class TrailHolder(binding: TrailViewBinding): ViewBindingHolder<out Identifiable<*>, TrailViewBinding>(binding) {
    override fun binding(position: Int) {
        // Position has corrected the actual position of this ViewHolder in the end of the table
        // The list: ListHelper tool cannot be used
        // Bind the view to the data}}/ / use
val itemAdapter = ItemAdapter()
recyclerView.adapter = itemAdapter
// Submit data to Adapter
itemAdapter.submitList(list)    
Copy the code

paging3 + DataBinding

  • When using Paging3 and DataBinding, you can also use the automatic binding function:
/ / only change AutomaticBindingAdapter to PagingAutomaticBindingAdapter can.
// the rest is exactly the same as the AutomaticBindingAdapter.
class ItemAdapter : PagingAutomaticBindingAdapter<Item, ItemBinding>(a)Copy the code

paging3 + ViewBinding

  • The PagingBindingAdapter is still available for easy extension
// Replace MutableBindingAdapter with PagingBindingAdapter.
class ItemAdapter : PagingBindingAdapter<Item, ItemBinding, ItemHolder>(::ItemHolder)
Copy the code

The introduction of

  1. Add the following code to the appropriate location in your Build. gradle file at the root of your Android project:
allprojects {
    repositories {
        maven { url 'https://jitpack.io'}}}Copy the code
  1. Add the following code to the appropriate location in the build.gradle file of the corresponding Android module in your Android project:
implementation 'cn.numeron:recyclerview.adapter:latest_version'
Copy the code