Has anyone using RecyclerView
found a way to set an onClickListener
to items in the RecyclerView
?
I thought of setting a listener to each of the layouts for each item but that seems a little too much hassle
I'm sure there is a way for the RecyclerView
to listen for the onClick
event but I can't quite figure it out.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
From most of the answers above, they seem to be setting their onclicklisteners to individual items. However, the solution am about to offer is very simple but yet not intuitive to many. Many are forgetting that the other components are always in a parent component which is used to display items in the List or Recycler views. This solution is just about setting a single onclick listener to this parent view and the turn is played. The solution also includes a way to pass the position of the item being clicked on from the list or recycler view. Here, our main rootview is a CardView from the android support library. Here is sample code
If you want to catch click event On Individual items then just implement
OnClickListener
inViewHolder
class and then set click listeners on individual views or wholeitemView
.Following example shows the same
This is what I ended up needing, in case someone finds it useful:
Source: http://blog.csdn.net/jwzhangjie/article/details/36868515
There is far easier way to do this. Just apply on click in
onBindViewHolder
on root view.Consider this is your view for adapter,
Then do following in your adapter
All the answers posted so far are great solutions, however if you do not want to deal with too many implementation details, and just want it to work similarly to how ListView does, I would recommend using TwoWay-View, as seen here:
https://github.com/lucasr/twoway-view
Note that this implementation also supports long press on items, as well as support for pressed states (which is something important that other solutions to this question lack).
If you don't want to use the entire library, take a look at the ClickItemTouchListener class, which can be used as a standalone if needed. The only issue I found with it at the moment is with long press + scrolling, it seems to have incorrect behaviour.
Here is what I did. This solution supports both onClick and onLongClick on both RecyclerView Items and Views insides RecyclerView Items (internal views).
I tag viewHolder on the views of my choice :
And I use holder.getPosition() to retrieve the position in onClick() method (onLongClick is similar) :
A variant with getChildPosition also works. Please note that for the internal views, in onClick() use :
To my mind, the avantage of this solution is that when one clicks on the image, only the onclick() image listener is called whereas when I combined Jacob's solution for a RecyclerView Item view and my solution for internal views the RecyclerView Item view onclick() is also called (when click on image).