Xamarin Android: Create Android Click Event Using Recycler View

First, we create a Recycler View with use of List of items. I already explained it my last article. (refer to the below link)

After completing the Recycler View App creation, please follow the below steps to, easily, create a Click Event.

Step 1: Open Solution Explorer-> Project Name->MainActivity.cs. Add the following Namespaces. We ha already created all the adapter classes to pass the single Recycler value only. But now, we need to pass one more recycler value. Please see the following images:

code
C# Code

  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     SetContentView(Resource.Layout.Main);  
  5.     recyclerview = FindViewById < RecyclerView > (Resource.Id.recyclerview);  
  6.     List < ContactList > contact = new List < ContactList > ();  
  7.     contact.Add(new ContactList {  
  8.         ContactName = "Anbu", Number = "2564125624"  
  9.     });  
  10.     contact.Add(new ContactList {  
  11.         ContactName = "Arun", Number = "52459878788"  
  12.     });  
  13.     contact.Add(new ContactList {  
  14.         ContactName = "John", Number = "57879877878"  
  15.     });  
  16.     contact.Add(new ContactList {  
  17.         ContactName = "Peter", Number = "56478989798"  
  18.     });  
  19.     contact.Add(new ContactList {  
  20.         ContactName = "Ali", Number = "12345687945"  
  21.     });  
  22.     ContactListitems = new ContactListAdapter < ContactList > ();  
  23.     foreach(var s in contact) {  
  24.         ContactListitems.Add(s);  
  25.     }  
  26.     recyclerview_layoutmanger = new LinearLayoutManager(this, LinearLayoutManager.Vertical, false);  
  27.     recyclerview.SetLayoutManager(recyclerview_layoutmanger);  
  28.     recyclerview_adapter = new RecyclerAdapter(ContactListitems, recyclerview);  
  29.     recyclerview.SetAdapter(recyclerview_adapter);  
  30. }  
Step 2: Next step is to end of the Oncreate() to add new Recycle View variables.

code

C# Code
  1. public class RecyclerAdapter: RecyclerView.Adapter   
  2. {  
  3.     private ContactListAdapter < ContactList > Mitems;  
  4.     Context context;  
  5.     RecyclerView mrecyle;  
  6.     public RecyclerAdapter(ContactListAdapter < ContactList > Mitems, RecyclerView recyler) {  
  7.         this.Mitems = Mitems;  
  8.         NotifyDataSetChanged();  
  9.         mrecyle = recyler;  
  10.     }  
  11.     public class MyView: RecyclerView.ViewHolder {  
  12.         public View mainview {  
  13.             get;  
  14.             set;  
  15.         }  
  16.         public TextView mtxtcontactname {  
  17.             get;  
  18.             set;  
  19.         }  
  20.         public TextView mtxtcontactnumber {  
  21.             get;  
  22.             set;  
  23.         }  
  24.         public EventHandler ClickHandler {  
  25.             get;  
  26.             set;  
  27.         }  
  28.         public MyView(View view): base(view) {  
  29.             mainview = view;  
  30.         }  
  31.     }  
  32.     public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {  
  33.         View listitem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ContactLayout, parent, false);  
  34.         TextView txtcontactname = listitem.FindViewById < TextView > (Resource.Id.txtcontactname);  
  35.         TextView txtnumber = listitem.FindViewById < TextView > (Resource.Id.txtnumber);  
  36.         MyView view = new MyView(listitem) {  
  37.             mtxtcontactname = txtcontactname, mtxtcontactnumber = txtnumber  
  38.         };  
  39.         return view;  
  40.     }  
  41.     public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) {  
  42.         MyView myholder = holder as MyView;  
  43.         myholder.mtxtcontactname.Text = Mitems[position].ContactName;  
  44.         myholder.mtxtcontactnumber.Text = Mitems[position].Number;  
  45.         myholder.mainview.Click += Mainview_Click;  
  46.     }  
  47.     private void Mainview_Click(object sender, EventArgs e) {  
  48.         int position = mrecyle.GetChildAdapterPosition((View) sender);  
  49.         //int indexPosition = (Mitems.Count-0) - position;  
  50.         Console.WriteLine(Mitems[position].ContactName);  
  51.         //D  
  52.     }  
  53.     public override int ItemCount {  
  54.         get {  
  55.             return Mitems.Count;  
  56.         }  
  57.     }  
  58. }  
Step 3: Press F5 or Build and Run the Application.

output

Finally, We have successfully created the Xamarin Android Click Event using Recycler.