'Show item info on selected item in RecyclerView using Kotlin

I am using kotlin language with android studio. I want to get the properties of the element I clicked in the RecyclerView. Ben bu kod ile saderc id alabiliyorum

Ex: date

ListAdapter.kt

class ListAdapter(
    private val context: Context
) : RecyclerView.Adapter<ListAdapter.ListViewHolder>() {

    private var dataList = mutableListOf<Any>()
private lateinit var mListener: onItemClickListener

interface onItemClickListener {
    fun onItemClick(position: Int)
}

fun setOnItemClickListener(listener: onItemClickListener) {
    mListener = listener
}
    fun setListData(data: MutableList<Any>) {
        dataList = data
    }

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

    override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
        val question: Questionio = dataList[position] as Questionio
        holder.bindView(question)
    }

    override fun getItemCount(): Int {
        return if (dataList.size > 0) {
            dataList.size
        } else {
            return 0
        }
    }



inner class ListViewHolder(itemView: View, listener: onItemClickListener) :
    RecyclerView.ViewHolder(itemView) {
    fun bindView(questionio: Questionio) {
        itemView.findViewById<TextView>(R.id.txt_policlinic).text = questionio.policlinic
        itemView.findViewById<TextView>(R.id.txt_title).text = questionio.title
        itemView.findViewById<TextView>(R.id.txt_description).text = questionio.description
        itemView.findViewById<TextView>(R.id.txt_date).text = questionio.date
        itemView.findViewById<TextView>(R.id.txt_time).text = questionio.time

    }

    init {
        itemView.setOnClickListener {
            listener.onItemClick(adapterPosition)
        }
    }
}
}

My code in onCreateView inside list fragment.Edit ListFragment

recyclerView.layoutManager = LinearLayoutManager(requireContext())
            recyclerView.adapter = adapter
            observeData()
adapter.setOnItemClickListener(object : ListAdapter.onItemClickListener {
                override fun onItemClick(position: Int) {
                    showShortToast(position.toString())
                }
            })

this function is also my observationData(), I made new edits

private fun observeData() {
        binding.shimmerViewContainer.startShimmer()
        listViewModel.fetchQuestinData("questions", 
        requireContext())
            .observe(viewLifecycleOwner, {
                binding.shimmerViewContainer.startShimmer()
                binding.shimmerViewContainer.hideShimmer()
                binding.shimmerViewContainer.hide()
                adapter.setListData(it)
                adapter.notifyDataSetChanged()
            })
    }


Solution 1:[1]

You can pass highOrderFuction into the adapter then setonclickListener for any view you want. Like this:

class ListAdapter(
   private val context: Context,
   private val onItemClick:(questionio: Questionio)->Unit
) : RecyclerView.Adapter<ListAdapter.ListViewHolder>() {

 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_row, parent, false)
        return ListViewHolder(view,onItemClick)
    }
...



inner class ListViewHolder(itemView: View,private val onItemClick:(questionio: Questionio)->Unit) : RecyclerView.ViewHolder(itemView) {
        fun bindView(questionio: Questionio) {

            //set on any view you want
            itemView.findViewById<TextView>(R.id.root_view_id).
            setOnClickListener{onItemClick(questionio)} 

            itemView.findViewById<TextView>(R.id.txt_policlinic).text = 
            questionio.policlinic
            itemView.findViewById<TextView>(R.id.txt_title).text = questionio.title
            itemView.findViewById<TextView>(R.id.txt_description).text = 
            questionio.description
            itemView.findViewById<TextView>(R.id.txt_date).text = questionio.date
            itemView.findViewById<TextView>(R.id.txt_time).text = questionio.time
        }
    }



}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1