This adapter I treasure for a long time (nearly two years), continue to see other people send adapter related articles, but I always feel not my good, so today to share (propaganda), welcome to point out the lack of.

Source address: GitHub

function

  • There is no need to inherit Adapter, no need to determine the item type.
  • Support for headers and footers.
  • Supports automatic display of empty data interface.
  • Massive code reduction through Kotlin’s lambda.
  • Supports the global Item type
  • Diff refresh is supported

use

Add the dependent

implementation "com.dengzii.adapter:$latestVersion"
Copy the code

With lambda quick use, there is a quick binding of the layout of the four items.

adapter.setEnableEmptyView(true, SuperAdapter.Empty())
adapter.addViewHolderForType<SuperAdapter.Empty>(R.layout.item_empty){
    onBindData { _, _ -> 
        findView<View>(R.id.bt_refresh).setOnClickListener { 
            // refresh your data
        }
    }
}
adapter.setHeader("This is header", R.layout.item_header) {
    onBindData { data, _ ->
        findView<TextView>(R.id.tv_title).text = data
    }
}
adapter.setFooter(listOf("This"."is"."footer"), R.layout.item_section) {
    onBindData { data, _ ->
        findView<TextView>(R.id.tv_title).text = data.joinToString("")
    }
}
adapter.addViewHolderForType<Header>(R.layout.item_header) {
    val title = findView<TextView>(R.id.tv_title)
    val content by lazyFindView<TextView>(R.id.tv_content)
    onBindData { data, _ ->
        title.text = data.title
        content.text = data.content
    }
}
Copy the code

Or don’t use lambda

val adapter = SuperAdapter(listOf("Item 1"."Item 2"."Item 3"))
adapter.addViewHolderForType(String::class.java, ItemViewHolder::class.java)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter

class ItemViewHolder(parent: ViewGroup) : AbsViewHolder<String>(parent) {
    private lateinit var mTextView:TextView 
    override fun onCreate(parent: ViewGroup) {
        mTextView = TextView(context)
        mTextView.layoutParams = getLayoutParam(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                )
        setContentView(mTextView)
    }

    override fun onBindData(data: String, position: Int) {
        mTextView.text = data}}Copy the code

There is no need to inherit the SuperAdapter, but the abstract class AbsViewHolder is implemented and inherited for each Item, and the layout and binding View, data are set in the modified class.

AbsViewHolder represents an Item type, where the generic T is the entity class corresponding to that Item

Class SuperAdapter

Adapter constructor

public SuperAdapter(List<Object> data)
Copy the code

Bind the ViewHolder to the entity type

public void addViewHolderForType(Class<? > type, Class<? extends AbsViewHolder> holder)Copy the code

Set the Item click event

 public void setOnItemClickListener(OnItemClickListener listener)
 
 public interface OnItemClickListener{
     void onItemClick(View v, Object itemData, int position);
 }
Copy the code

The AbsViewHolder class is used

Each AbsViewHolder represents an item type.

Constructor, which must override the constructor with arguments otherwise it cannot be used. Parent is the same as parent in Adapter#onCreateViewHolder

 public AbsViewHolder(@NonNull ViewGroup parent) 
Copy the code

The onCreate method is called when an Item is created, where parent is the container layout for the Item

 public abstract void onCreate(@NonNull ViewGroup parent);
Copy the code

Data binding

public abstract void onBindData(@NonNull T data, int position);     
Copy the code

Sample

Set the Adapter

var data:List<Any> ... Val adapter = SuperAdapter (data)/class/bind data to the ViewHolder adapter. AddViewHolderForType (Item: : class. Java, ItemViewHolder::class.java) adapter.addViewHolderForType(Header::class.java, HeaderViewHolder::class.java) adapter.addViewHolderForType(Section::class.java, SectionViewHolder::class.java) adapter.setOnItemClickListener(object : SuperAdapter.OnItemClickListener { override fun onItemClick(v: View? , itemData: Any? , position: Int) { } }) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = adapterCopy the code

Inheritance AbsViewHolder

class ItemViewHolder(parent: ViewGroup) : AbsViewHolder<Item>(parent) {
    private lateinit var mTvTitle: TextView
    private lateinit var mTvContent: TextView
    private lateinit var mIvImage:ImageView

    override fun onCreate(parent: ViewGroup) {
        setContentView(R.layout.item_item)
        mTvTitle = findViewById(R.id.tv_title)
        mTvContent = findViewById(R.id.tv_content)
        mIvImage = findViewById(R.id.iv_img)
    }

    override fun onBindData(data: Item, position: Int) {
        mTvTitle.text = data.title
        mTvContent.text = data.content
        mIvImage.setImageResource(data.img)
    }
}
Copy the code