kotlin android中Recyclerview的适配器类

【kotlin android中Recyclerview的适配器类】须知少年凌云志,曾许人间第一流。这篇文章主要讲述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. }


    推荐阅读