-1

This is making me go bananas !! Swear i will never eat bananas again !! I am trying work on a Kotlin/Fragment/RecyclerViewAdapter/OnItemClickListener but it is not working

I am trying to make the ItemOnClickListener to work between the RecyclerView Adapter and the Fragment. I have indicated in the code where the problems are.

FRAGMENT

class VCTask : Fragment() {
    private val TAG = "VCTask"
    private lateinit var mContext: Context
    private var _binding: FTaskBinding? = null
    private val binding get() = _binding!!

    private val exampleList = generateDummyList(500)
    ***private val adapter = ExampleAdapter(exampleList,this) <-----ERROR "this" in Fragment is rejected obviously 
it refers to Activity..tried to use context etc but it is expecting ContentListener

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FTaskBinding.inflate(inflater, container, false)
        val view = binding.root
        return view
    }

    private fun fragbind(view:View) {
        mContext = this.requireContext()
        binding.recyclerView.adapter = adapter
        binding.recyclerView.layoutManager = LinearLayoutManager(mContext)
        binding.recyclerView.setHasFixedSize(true)

    }

    fun onItemClicked(position: Int) { <------------------------ERROR: I want this to work
        mContext = this.requireContext()
        Toast.makeText(mContext, "Item $position clicked", Toast.LENGTH_SHORT).show()
        val clickedItem = exampleList[position]
        clickedItem.text1 = "Clicked"
        adapter.notifyItemChanged(position)
    }

}

RECYCLER ADAPTER



class ExampleAdapter(private val exampleList: List<ExampleItem>,listener: ContentListener) : <---LISTENER 
ADDED AS VARIABLE..WHAT TO SHOW IN FRAGMENT ??
RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
        val itemView = ExampleItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ExampleViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
        val currentItem = exampleList.get(position)
            holder.binding.imageView.setImageResource(currentItem.imageResource)
            holder.binding.textView1.text = currentItem.text1
            holder.binding.textView2.text = currentItem.text2
    }

    override fun getItemCount(): Int = exampleList.size

    inner class ExampleViewHolder(val binding: ExampleItemBinding) : RecyclerView.ViewHolder(binding.root) {
        val imageView: ImageView = binding.imageView
        val textView1: TextView = binding.textView1
        val textView2: TextView = binding.textView2

        fun bind(listOfData: 
         ArrayList<ExampleItem>, listener: ContentListener) {  <-----------IS THIS CORRECT ??  
Because adapterPosition is deprecated

            val item = listOfData[adapterPosition]
            itemView.setOnClickListener {
                listener.onItemClicked(listOfData.get(adapterPosition))
            }
        }
    }

    public interface ContentListener {
        fun onItemClicked(item: ExampleItem)
    }

}

Appreciate your input...i have spent too much time on this...

1 Answers1

0

You need to declare and implement your interface in your fragment, like this:

binding.recyclerView.adapter.setOnclickListiner(mOnAdapterClickListener)

and then define and implement like this..

private val mOnAdapterClickListener = object : ContentListener {
    override fun onItemClicked(item: ExampleItem) {

    //do your stuff

}
David Buck
  • 3,752
  • 35
  • 31
  • 35