MagicAdapter

RecyclerView is a super lightweight tool library that can quickly realize RecyclerView complex adapters.

features

  • Based on the Kotlin language
  • Based on the Databinding
  • There is no longer a maintenance Adapter; there is only one MagicAdapter globally
  • You no longer need to maintain the ViewHolder
  • Multiple entry types are supported
  • Some basic callbacks are provided
  • Easily extend Header and Footer
  • Easily expand RecycleView animations and events
  • No reflection, very efficient
  • Super lightweight, less than 34 KB
  • Minimum compatible with Android SDK: 14

integration

1. Add the jCenter repository to root’s build.gradle

allprojects {
    repositories{... jcenter() } }Copy the code

2. Enable databinding in app’s build.gradle

android {
    ...
    dataBinding {
        enabled = true}}Copy the code

3. Add dependencies to app’s build.gradle (please use the latest version)

dependencies{... implementation'com. Xuyefeng: magicadapter: 1.0.1'
}
Copy the code

use

1. Create RecyclerView Item layout file image_item_layout.xml

<?xml version="1.0" encoding="utf-8"? >
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="item"
            type="com.blue.helloadapter.ImageItem" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</layout>
Copy the code

Note that the name of the XXXItem declaration here must be item


2. Create an XXXItem (ImageItem) corresponding to the item layout file

class ImageItem(
        val resId: Int
) : BaseItem() {

    override fun getLayout(a): Int = R.layout.image_item_layout

    override fun onBinding(binding: ViewDataBinding) {
        (binding as ImageItemLayoutBinding).apply {
            iv.setImageResource(resId)
            iv.setOnClickListener {
                Toast.makeText(iv.context, "click image on ${getViewHolder()? .adapterPosition}", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
Copy the code

Here’s a description of BaseItem:

  • You can get a RecyclerView binding Adapter
  • You can get the ViewHolder for the item
  • You can get the Databinding for item

3. Create TextItem and ButtonItem in the same way, and then add data

val adapter = MagicAdapter()
adapter.addItem(ImageItem(R.drawable.s1))
adapter.addItem(TextItem())
adapter.addItem(ButtonItem())
recyclerView.adapter = adapter
Copy the code

4. If you need a callback, go like this

adapter.onItemClickListener = object : OnItemClickListener {
    override fun onItemClick(holder: ItemViewHolder)
          val position = holder.adapterPosition
          val item = holder.item
          valbinding = holder.binding ... }}Copy the code

That’s it. Adding a complex adapter to a RecyclerView can be so simple.

Finally add: github address portal as you like to start it