Posted on 113 Comments

A generic ‘RecyclerView Adapter’ for Xamari.Android

The idea is to create a base RecyclerView that can be used many times in your android application. To read more about the component visit this page.

Let’s start!

The View Holder,

Let’s start with View Holder that will be also a generic one. As discribed in the documentation page of the RecyclerView mentioned in the introduction, a new instance of the View Holder is created for each item in the list.

We create our abstract class (Of the View Holder) that defines two methods :
InitComponents() : Will be called to initialize the components of the ViewCard.
InitEvents() : Will be called to initialize the events of the ViewCard.

The ViewCardAction class is supposed to represent any action can be done in the ViewCard, and here its definition :

The Adapter,

The adapter, that provides the data source of the RecyclerView and binds each View with the data item, is also an abstract class that inherates from RecyclerView.Adapter.

Let’s see the full class and explain it :

As you see, the adapter defines a DataSource property of type DataCollection<T>. The DataCollection is a generic collection implements both interfaces IList and ICollection. Its role is to manage the items list of the adapter (add, delete, insert, count …). It should be like :

The DataCollectionItem defines the element of the collection.
– The property IsSelected indicates if the item is selected. (Option)
– The property IsExpanded indicates if the item is expanded. (Option)

All the events args are :

Thank you all, I hope you found something of important here 🙂 ..

I will publish a Simple Project as soon as possible..