I've created a Carousel using MotionLayout by following this guide: https://developer.android.com/training/constraint-layout/motionlayout/carousel
I've gotten everything from this guide working - I can swipe through a carousel of items.
However, I'm having trouble adding an onClickListener to my carousel's items. I've tried setting the onClickListener in the adapter like so:
carousel.setAdapter(object : Carousel.Adapter {
override fun populate(view: View?, index: Int) {
view.setOnClickListener {
// do something
}
}
})
However, this makes the carousel unresponsive to swiping. When I try to swipe the carousel it executes my onClick function but doesn't execute my onSwipe transition.
My question is, what is the proper way to set an onClick function for a Carousel item?
While searching for an answer, I've seen a few posts that reference Carousel.setOnItemClickListener(). This is exactly what I need, but it seems to have been deprecated.
I've also seen posts to say to override onTouchListener and run your onClick code on the ACTION_UP event. I've tried a couple variations of this code snippet, but haven't been able to get it to work:
carousel.setAdapter(object : Carousel.Adapter {
override fun populate(view: View?, index: Int) {
view.setOnTouchListener { view, motionEvent ->
if(motionEvent.action == MotionEvent.ACTION_UP) {
// do something
}
view.performClick()
}
}
})
Would really appreciate any help!