Adapter class for Recyclerview in kotlin android


/ Published in: Other
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.