androidmobileandroid-recyclerviewandroid-adapter

No item selection in RecyclerView


I have made a vertical recyclerview in my program. When i click on any recyclerView items i don't see the on click listener working. Here is my code for the Adapter class:

class BoardItemsAdapter(private var list : ArrayList<Board>):RecyclerView.Adapter<BoardItemViewHolder>(){
private var onClickListener: OnClickListener? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BoardItemViewHolder {
        val from  = LayoutInflater.from(parent.context)
        val binding = ItemBoardBinding.inflate(from , parent, false)
        return BoardItemViewHolder(parent.context, binding)
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: BoardItemViewHolder, position: Int) {
        holder.bindBoardItem(list[position])
    }
    interface OnClickListener : OnItemClickListener {
        fun onClick(position: Int, model: Board)
    }
    fun setOnClickListener(onClickListener: OnClickListener){
        this.onClickListener = onClickListener
    }

}

BoardItemViewHolder:

class BoardItemViewHolder( private val context: Context, private val binding : ItemBoardBinding):RecyclerView.ViewHolder(binding.root) {
    fun bindBoardItem(list : Board){

// changing board name acording to database
        binding.tvBoardName.text = list.name
// using "Glide" 3rd party library to loading image for board image
Glide.with(context).load(list.image).centerCrop().placeholder(R.drawable.ic_board).into(binding.ivBoardImage)    
// changing owner of the board acording to database
        binding.tvCreatedBy.text = "Created by ${list.owner}"
    }

}

In my MainActivity:

/*
....
*/
recyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false)
            recyclerView.setHasFixedSize(true)
            val adapter = BoardItemsAdapter(boardsList)
            recyclerView.adapter = adapter
            adapter.setOnClickListener(object: BoardItemsAdapter.OnClickListener{
                override fun onClick(position: Int, model: Board) {
                    startActivity(Intent(this@MainActivity, TaskList::class.java))
                }
            })
/*
....
*/

I thing something is wrong in BoardItemsAdapter, but i can't figure it out.


Solution

  • You are not setting the onClick listener to items, you just store it inside the adapter.

    class BoardItemsAdapter(private var list : ArrayList<Board>) : RecyclerView.Adapter<BoardItemViewHolder>() {
        
        private var onClickListener: OnClickListener? = null
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BoardItemViewHolder {
            val from  = LayoutInflater.from(parent.context)
            val binding = ItemBoardBinding.inflate(from, parent, false)
            // Root is just an example.
            binding.root.setOnClickListener(this@BoardItemsAdapter.onClickListener)
    
            return BoardItemViewHolder(parent.context, binding)
        }
    
        override fun onBindViewHolder(holder: BoardItemViewHolder, position: Int) {
            holder.bindBoardItem(list[position])
        }
    
        fun setOnClickListener(onClickListener: OnClickListener){
            this.onClickListener = onClickListener
        }
    
    }
    
    class BoardItemViewHolder( private val context: Context, private val binding : ItemBoardBinding): RecyclerView.ViewHolder(binding.root) {
        fun bindBoardItem(list : Board){
            binding.tvBoardName.text = list.name
            Glide.with(context).load(list.image).centerCrop().placeholder(R.drawable.ic_board).into(binding.ivBoardImage)    
            binding.tvCreatedBy.text = "Created by ${list.owner}"
        }
    }
    

    UPDATE:

        fun bindBoardItem(list : Board, onClickListener: View.OnClickListener) {
            binding.tvBoardName.text = list.name
            Glide.with(context).load(list.image).centerCrop().placeholder(R.drawable.ic_board).into(binding.ivBoardImage)    
            binding.tvCreatedBy.text = "Created by ${list.owner}"
            binding.root.setOnClickListener(onClickLis)
        }
    
        override fun onBindViewHolder(holder: BoardItemViewHolder, position: Int) {
            holder.bindBoardItem(list[position]) { this@BoardItemsAdapter.onClickListener(list[position]) }
        }
    
    class BoardItemViewHolder(
        private val context: Context, 
        private val binding : ItemBoardBinding,
        private val onItemClickListener: BoardItemsAdapter.OnClickListener 
        private var item : Board
        ): RecyclerView.ViewHolder(binding.root) {
    
        init {
            binding.root.setOnClickListener { onItemClickListener(item) }
        }