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
For me, this is the best way:
Here is what I did Read more & download the gist here
Adding the same here
CustomItemClickListener.java
ItemsListAdapter.java
I'm aware there are a lot of answers, but I thought I might just provide my implementation of it as well. (Full details can be found on another question I answered).
So, to add a click listener, your inner
ViewHolder
class needs to implementView.OnClickListener
. This is because you will set anOnClickListener
to theitemView
parameter of theViewHolder
's constructor. Let me show you what I mean:The only other things you need to add are a custom interface for your
Adapter
and a setter method:So your new, click-supporting
Adapter
is complete.Now, let's use it...
It's basically how you would set up a normal
Adapter
, except that you use your setter method that you created to control what you will do when your user clicks a particular list item.You can also look through a set of examples I made on this Gist on GitHub:
https://gist.github.com/FarbodSalamat-Zadeh/7646564f48ee708c1582c013e1de4f07
You can pass a
clickListener
toAdapter
.In your
Activity
:then pass it to
Adapter
:In
Adapter
'sonCreateViewHolder
:I have developed a light weighted library for android, you can visit https://github.com/ChathuraHettiarachchi/RecycleClick
and follow for following sample
public class MyCustomAdapter extends RecyclerView.Adapter
<
AdapterMyCustomAdapter.ViewHolder> {}