kotlin android中Recyclerview的适配器类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kotlin android中Recyclerview的适配器类相关的知识,希望对你有一定的参考价值。

This the base code for creating adapter class which is used for filling recyclerview in android.
This snippet is written in kotlin but it is same to the java version
  1. class ClassName(val list:ArrayList<Type of your list>):RecyclerView.Adapter<ClassName.ViewHolder>() {
  2.  
  3.  
  4. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClassName.ViewHolder {
  5. val v = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
  6. return ViewHolder(v)
  7. }
  8.  
  9. override fun getItemCount(): Int {
  10. return list.size
  11. }
  12.  
  13. override fun onBindViewHolder(holder: ClassName.ViewHolder, position: Int) {
  14. holder.bindItems(list[position])
  15. }
  16. class ViewHolder(itemview: View):RecyclerView.ViewHolder(itemview)
  17. {
  18.  
  19. fun bindItems(t: type of your list){
  20.  
  21. }
  22. }
  23. }

以上是关于kotlin android中Recyclerview的适配器类的主要内容,如果未能解决你的问题,请参考以下文章