Answers for "recyclerview adapter kotlin"

3

recyclerview android kotlin

data class :
	data class model(var number:Int)
    
    
    
    
    
    
Adapter Class :
    class adapter(private val listt:ArrayList<model>): RecyclerView.Adapter<adapter.ViewHolder>() {

        class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var text = itemView.findViewById<TextView>(R.id.text)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val inflater= LayoutInflater.from(parent.context).inflate(R.layout.fragment_item_view,parent,false)
            return ViewHolder(inflater)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val current = listt[position]
            holder.text.text = current.number.toString()
        }

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

    }





In Activity :
    val num = ArrayList<model>()
    for (i in 1..10)
        num.add(model(i))

    val adapter = adapter(num)
    binding.recyclerView.adapter = adapter
    binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
Posted by: Guest on November-07-2021
1

recyclerview adapter in kotlin

class CustomAdapter(private val dataSet: Array<String>) :
        RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
      
    }


    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {

      
    }

    override fun getItemCount() {
    }

}
Posted by: Guest on May-31-2021
0

recyclerview onCreateViewHolder kotlin

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // Inflate the custom view from xml layout file
        val v: View = LayoutInflater.from(parent?.context)
            .inflate(R.layout.custom_view, parent, false)

        // Return the view holder
        return ViewHolder(
            v
        )
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // Display the current user full name and location in view holder custom view
        holder?.name?.text = list.get(position).description

    }
Posted by: Guest on August-04-2020

Code answers related to "recyclerview adapter kotlin"

Browse Popular Code Answers by Language