RecyclerView adapter implemented by Android

  • 2021-12-11 18:59:55
  • OfStack

Directory function Use The role of SuperAdapter-like The role of AbsViewHolder-like Sample

I have treasured this adapter for a long time (nearly two years), and I keep seeing articles about adapters sent by others, but I always feel that it is not as good as mine, so I take it out today to share (publicize) 1, and welcome you to correct your shortcomings.

Source address: GitHub

Function

There is no need to inherit Adapter, and there is no need to judge item type. Support page headers and footers. Support automatic display of empty data interface. The code is greatly reduced through lambda of Kotlin. Support for global Item types Support diff refresh

Use

Add dependencies


implementation "com.dengzii.adapter:$latestVersion"

With the quick use of lambda, four layouts of item are quickly bound here.


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

Or do not 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
 }
}

There is no need to inherit SuperAdapter, but you need to implement and inherit the inherited abstract class AbsViewHolder for each Item, and set the layout and bind View and data in the class change.

AbsViewHolder represents one type of Item, where the generic T is the entity class corresponding to the Item

Role of SuperAdapter-like

Adapter Constructor


public SuperAdapter(List<Object> data)

Binding ViewHolder to Entity Type


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

Setting Item Click Events


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

Role of AbsViewHolder-like

Each AbsViewHolder represents one item type.

Constructor, the constructor with parameters must be overridden, otherwise it cannot be used, parent 1 in parent and Adapter # onCreateViewHolder


 public AbsViewHolder(@NonNull ViewGroup parent) 

Call the onCreate method when creating an Item, where parent is the container layout of the item


 public abstract void onCreate(@NonNull ViewGroup parent);

Bind data


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

Sample

Setting Adapter


var data:List<Any>
...
val adapter = SuperAdapter(data)
//  Bind data classes to  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 = adapter

Inheriting AbsViewHolder


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
 }
}
0

The above is the Android implementation of the RecyclerView adapter details, more information about the RecyclerView adapter please pay attention to other related articles on this site!


Related articles: