RecyclerView itemClickListener in Kotlin
I have a little bit different approach. You can create an extension for your ViewHolder
fun <T : RecyclerView.ViewHolder> T.listen(event: (position: Int, type: Int) -> Unit): T {
itemView.setOnClickListener {
event.invoke(getAdapterPosition(), getItemViewType())
}
return this
}
Then use it in adapter like this
class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
val items: MutableList<String> = arrayListOf()
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder? {
val inflater = LayoutInflater.from(parent!!.getContext())
val view = inflater.inflate(R.layout.item_view, parent, false)
return MyViewHolder(view).listen { pos, type ->
val item = items.get(pos)
//TODO do other stuff here
}
}
override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {
}
override fun getItemCount(): Int {
return items.size()
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
}
}
I am working with my colleagues on library providing such extensions.
RecyclerView onitemClickListener in Kotlin
Follow below steps:
Step - 1: You have to implements OnChapterClick
in your MainActivity2
class MainActivity2: AppCompatActivity, OnChapterClick {
....
override fun onItemClick(list: Model, position: Int ) {
//Implement your logic here
}
}
Step - 2: Change your CustomeAdapter
's constructor to accept OnChapterClick
instead of int
class CustomeAdapter(val ctx: Context, var clickListener: OnChapterClick, var chapterlist:ArrayList<Model>) : RecyclerView.Adapter<ViewHolder>() {
....
}
Step - 3: Initialize the adapter with this
instead of R.layout.row
like below:
val adapter = CustomeAdapter(applicationContext, this@MainActivity2, chapterlist)
How to add a click listener to my recycler view (Android kotlin)
Try this
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textViewName.text = seriesList[position].name
holder.textViewNameDesc.text = seriesList[position].desc
holder.textViewName.setOnClickListener {
Toast.makeText(context,"clicked",Toast.LENGTH_SHORT).show()
}
}
RecyclerView Item Click Listener the Right Way
You need to check this tutorial here for better understanding on how you can achieve the behaviour that you want.
In case of handling the onClickListener
from your activity you need to work based on a callback implementation with an interface. Pass the interface from the activity to your adapter and then call the callback function from your adapter when some items are clicked.
Here's a sample implementation from the tutorial.
Let us first have the interface.
public interface OnItemClickListener {
void onItemClick(ContentItem item);
}
You need to modify your adapter to take the listener as the parameter like the one stated below.
private final List<ContentItem> items;
private final OnItemClickListener listener;
public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
this.items = items;
this.listener = listener;
}
Now in your onBindViewHolder
method, set the click listener.
@Override public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(items.get(position), listener);
}
public void bind(final ContentItem item, final OnItemClickListener listener) {
...
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
Now setting the adapter in your RecyclerView
.
recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
@Override public void onItemClick(ContentItem item) {
Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
}
}));
So the whole adapter code looks like the following.
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(ContentItem item);
}
private final List<ContentItem> items;
private final OnItemClickListener listener;
public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
this.items = items;
this.listener = listener;
}
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false);
return new ViewHolder(v);
}
@Override public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(items.get(position), listener);
}
@Override public int getItemCount() {
return items.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView name;
private ImageView image;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
}
public void bind(final ContentItem item, final OnItemClickListener listener) {
name.setText(item.name);
Picasso.with(itemView.getContext()).load(item.imageUrl).into(image);
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
}
}
Related Topics
Extending Application to Share Variables Globally
How to Catch Sigsegv (Segmentation Fault) and Get a Stack Trace Under Jni on Android
Display Fragment Viewpager Within a Fragment
Access the Http Response Headers in a Webview
Could Not Find Com.Android.Tools.Build:Aapt2:3.2.0
Why Do I Get Access Denied to Data Folder When Using Adb
Checking Toast Message in Android Espresso
How to Group Radiobutton from Different Linearlayouts
How to Write Vertically in a Textview in Android
What Is the Real Android Studio Gradle Version
Indoor Positioning System Based on Gyroscope and Accelerometer
Usb Device Access Pop-Up Suppression
How to Disable Past Dates in Android Date Picker
Unicode Characters Not Displayed in Textview.Settext
Does Android Keep the .Apk Files? If So Where