Inflating ItemView for RecyclerView

Skele·2024년 3월 21일
0

Android

목록 보기
2/15

Two static inflate() methods


1. inflate(inflater)

This creates an instance of the binding class for the "Activity" to use.

2. inflate(inflater, parent, attachToParent)

This creates an instance of the binding class for the "Fragment" to use.
Inflating View for ViewHolder in Adapter uses this inflate()

class MainViewAdapter(private val context : Context, private val list:List<DtoToDo>) : RecyclerView.Adapter<MainViewHolder>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
        val inflater = LayoutInflater.from(context)
        return MainViewHolder(MainListItemBinding.inflate(inflater, parent, false)) // gotta attach to parent
    }

    override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
        holder.bindToData(list[position])
    }

    override fun getItemCount(): Int {
        return list.size
    }
}

reference

profile
Tireless And Restless Debugging In Source : TARDIS

0개의 댓글