I am working in Android Studio on a Kotlin project. I have defined my own Adapter and implemented a RecyclerView in my Activity. The RecyclerView populates the data correctly and allows me to scroll, but it doesn't seem to be registering the clicks for the onItemClick(position: Int) method.
Currently the onItemClick method is declared in the Adapter as an interface RecyclerViewEvent. This allows me to implement a listener in my Activity when declaring and attaching the Adapter. I then implement the onItemClick method in my Activity.
When I run the project in the emulator, it creates the RecyclerView and populates it with the correct data. I am able to scroll along it (up and down), but nothing happens when I click on the rows. I can hear that it is 'clicking', so I know it is receiving the touch. I've tried doing just a simple toast message and starting a new intent (which is what I need it to do anyway) for the onItemClick method.
There are no errors thrown and it doesn't break the program. Not sure what else to try with this.
Here is my Adapter:
class GuidedMeditationAdapter(
private val data: List<GuidedMeditationData>,
private val listener: RecyclerViewEvent,
) : RecyclerView.Adapter<GuidedMeditationAdapter.ViewHolder>(){
inner class ViewHolder(view: View): RecyclerView.ViewHolder(view), View.OnClickListener{
val textView: TextView = view.findViewById(R.id.gmrowtextview)
init {
view.setOnClickListener(this)
}
override fun onClick(v: View?) {
val position = bindingAdapterPosition
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflatedView: View = LayoutInflater.from(parent.context)
.inflate(R.layout.fragment_meditation_recyclerview_row,parent,false)
return ViewHolder(inflatedView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val selection: GuidedMeditationData = data[position]
holder.textView.text = selection.trackName
holder.textView.setBackgroundResource(selection.gmImages)
holder.textView.setOnClickListener{listener}
}
override fun getItemCount(): Int {
return data.size
}
interface RecyclerViewEvent {
fun onItemClick(position: Int)
}
}
Which is then implemented into my Activity like this:
class GuidedMeditationActivity : AppCompatActivity(),
GuidedMeditationAdapter.RecyclerViewEvent {
private val data = createData()
private lateinit var listener: OnClickListener
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_guided_meditation)
val recyclerView: RecyclerView = findViewById(R.id.gm_recyclerview)
recyclerView.adapter = GuidedMeditationAdapter(data,this)
recyclerView.layoutManager = LinearLayoutManager(this)
}
override fun onItemClick(position: Int) {
val userPick = data[position]
val intent = Intent(this,GuidedMeditationExoPlayer::class.java)
startActivity(intent)
Toast.makeText(this, userPick.trackName,Toast.LENGTH_SHORT).show()
}
private fun createData(): List<GuidedMeditationData> {
val title = GuidedMeditationDataRepo.gmNames
val trackId = GuidedMeditationDataRepo.soundcloudTrackID
val img = GuidedMeditationDataRepo.gmImgs
val audIds = GuidedMeditationDataRepo.gmGoogleAudIds
val vidIds = GuidedMeditationDataRepo.gmGoogleVidIds
val gmData = ArrayList<GuidedMeditationData>()
for (i in 0..5) {
gmData.add(
GuidedMeditationData(
title[i],
trackId[i],
img[i],
audIds[i],
vidIds[i]
)
)
}
return gmData
}
}
Thank you in advance for reading and for your help!
If I understand your question, you want to add an action when you click on an item in your recyclerview, depending on position. I'm not an expert, I began recently coding with Kotlin.
Concerning your adapter, it seems to be ok, but in your activity, if you want an action depending on position, you have to declare all.
For example, for position 0 (first position) => you want "action 1". For position 1 => you want "action 2".
So, I suggest you to use "when" condition for each position in your recyclerView
Like this, in your activity :
override fun onItemClick(position: Int) {
when (position) {
0 -> startActivity(Intent(this,GuidedMeditationExoPlayer::class.java))
1 -> Toast.makeText(this, userPick.trackName,Toast.LENGTH_SHORT).show()
}
Hope this will help
EDIT : So, let's see your adapter.
You can delete your onClick function from your adapter, because your onItemClick function in Activity is enough.
If you do that, you dont need your interface RecyclerViewEvent either, so you can delete it.
In your init, replace yours by : val position = adapterPosition listener.onClick(position)
In your onBindViewHolder, it's not necessary to input a setOnClickListener. Your init and onClick in the Activity is enough.
In your Activity, add onItemClick function with the when method.
Hope it will help this time ! (And my bad for the arrow...)