I got crazy ı am not able find solution. Please help me. 2 days ı am looking for solution. I clearly watn that if I click any recylerView Item starting a new activity. Each item has to own its activity. Thanks in advance
CodePudding user response:
If you put a list object to adapter like param, you can get position of item, and use this position to start activity you want.
something like this:
class Adapter(
private val list: List<YourModel>,
val clickListener: RecycleViewOnClickListener
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
...
}
in your bindViewHolder:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is ViewHolder) {
holder.itemView.setOnClickListener {
clickListener.onItemClick(position)
}
}
}
in your activity, get model from list with your position:
override fun onItemClick(pos: Int) {
val model = yourList[pos]
//start new activity
}
CodePudding user response:
You can do this by using high order function it will be easy
In you adapter class (pass list and high order function as callback)
class YourAdapter(private val itemList: List<Model>, private val onItemClicked: (item) -> Unit) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// your code
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
onItemClicked(itemList[position])
}
In your activity/Fragment where you are assigning adapter assign in this way.
whenever item is clicked it will land here, and you can start your activity, you will get item also from adapter on which user clicked
YourAdapter(list) { item ->
//start activity
}
Note: Always write some code with question, it will be easy for us to answer you and guide you what is wrong in your code.
Happy Coding!