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
This is what I do to reuse
OnClickListener
in ViewHoder Take itemlayout's parent
in onBindViewHolder set tag as position
and in Onclick
This is what worked for me. Attach the
OnClickListener
to theonBindView
. I don't really know if this will impact the performance, but it seems to work fine with little code.I have nice solution for
RecyclerView
'sonItemClickListener
for the items and subitemsStep 1- Create an interface
Step 2- Then use it in adapter's
onBindViewHolder
method in the following wayStep 3- find and setup recycler view in activity or fragment where you are using this
Step 4- Finally implement interface in activity or fragment where you are using the recyclerview
The
RecyclerView
does not have aOnClickListener
and will have to implement it ourselves.I like to add a
OnItemClickListener
interface inAdapter
with anonClick
method invoked when you click on the item view from theViewHolder
. Thus the responsibility of managing the click on an item is outside theViewHolder
andAdapter
. Will the activity or fragment which will decide what to doAdd an interface to the listener and the listener object.
We capture the click of the root view of the item and when the callback is triggered
onClick
listener call on the adapter .Since the activity or fragment , fragment in our case , we assign a listener to the adapter and the onClick callback we will get the selected item by position and opened a detailed activity of item.
Way too simple and effective.
Instead of implementing interface
View.OnClickListener
inside view holder or creating and interface and implementing interface in your activity - I used this code for simple onOnClickListener
implementation.Unfortunately
RecyclerView
is missing a couple of features thatListView
had built-in. For example the ability to add anOnItemClickListener
that triggers when an item is clicked.RecyclerView
allows you to set anOnClickListener
in your adapter, but passing on that click listener from your calling code, to the adapter and to theViewHolder
, is complicated for catching a simple item click.You also need to define
R.id.item_click_support
using ids.xml:The resulting code click listener now looks like this:
For Brief Explanation about recyclerview clicks please have a look at this littlerobots_blog