Home > other >  implement click listener on recycler view fragment
implement click listener on recycler view fragment

Time:07-31

im using click listener on adapter like this

in adapter :

private var onItemClickListener: ((TansitionModel) -> Unit)? = null


override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val article = differ.currentList[position]
    holder.itemView.apply {

        setOnClickListener { shit->
            onItemClickListener?.let {
                it(article)
            }
        }
    }
}


fun setOnItemClickListener(listener: (TansitionModel) -> Unit) {
    onItemClickListener = listener 
}

in fragement :

    financeTransitionAdapter.setOnItemClickListener {

        val extras = FragmentNavigator.Extras.Builder()
            .addSharedElement(
                requireView().findViewById(R.id.imgsd),
                "trans").build()

        findNavController(requireView())
            .navigate(R.id.action_FirstFragment_to_showTransitionFragment,
                null,
                null,
                extras
        )
    }

now with this architecture how can i pass multiple parameters like view and other data types (All together) from adapter click listener to fragment ??

CodePudding user response:

I don't have the full code of the adapter you are using but I will try to help with a piece of code from my project.

Firstly, what you need to do is create an interface like this:

interface AccountClickListener {
    fun onAccountClick(savedAccountModel: SavedAccountModel)
}

You can add as many parameters as you want to this function.

Now implement this interface in your adapter class constructor like this:

class ChangeAccountAdapter(
val context: Context,
val arrayList: ArrayList<SavedAccountModel>, 
val accountClickListener : AccountClickListener) 
: RecyclerView.Adapter<ChangeAccountAdapter.ChangeAccountHolder>(){
//your adapter class code
}

Your Holder Class should look something like this:

 inner class ChangeAccountHolder(val itemView : View) : RecyclerView.ViewHolder(itemView){
        val imageView : ImageView = itemView.findViewById(R.id.savedaccountimg)
        init {
          imageView.setOnClickListener{accountClickListener.onAccountClick(arrayList[adapterPosition])}
    }

Now, you can implement your click listener in your activity or fragment like this:

class Profile : Fragment(), AccountClickListener{
    override fun onAccountClick(savedAccountModel: SavedAccountModel) {
    // Code for what you want to do on clicking the item.
    }
}

And at last, you can define your adapter like this :

  val adapter = ChangeAccountAdapter(requireContext(),modelList,this)

CodePudding user response:

For this architecture, you need to just change the receiver for the lambda you use. Maybe like this.

private var onItemClickListener: ((TansitionModel,String, View) -> Unit) = {_, _, _ ->} // maybe use it like this instead of making it null

fun setOnItemClickListener(listener: (TansitionModel,String, View) -> Unit) {
    onItemClickListener = listener
}

And then call it with the aditional parameter. And you can then use them like this.

    financeTransitionAdapter.setOnItemClickListener { model, myString, view ->
        // use the variables
    }

However, i'd recommend using the interface approach as the other answer suggests

  • Related