RecyclerView in Xamarin Android
I started with a blank Android app.
Modified activity_main.xml to the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>Added a new layout file called Item.xml which defines each item as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="@+id/chat_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="12dp"
android:text="Hello" />
</LinearLayout>Added an adapter for the RecyclerView called MyAdapter.cs:
public class MyAdapter : RecyclerView.AdapterGet Shubham Agarwal’s stories in your inbox
Join Medium for free to get updates from this writer.
{
private List<string> items;
public MyAdapter(List<string> items)
{
this.items = items;
}
// A view holder class that holds a reference to a text view
public class MyViewHolder : RecyclerView.ViewHolder
{
public TextView textView;
public MyViewHolder(View itemView) : base(itemView)
{
textView = itemView.FindViewById<TextView>(Resource.Id.chat_item);
}
}
// A method that creates a view holder and inflates a layout for each item
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Item, parent, false);
MyViewHolder viewHolder = new MyViewHolder(itemView);
return viewHolder;
}
// A method that binds the data to the view holder
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MyViewHolder myHolder = holder as MyViewHolder;
string item = items[position];
myHolder.textView.Text = item;
}
public override int ItemCount
{
get { return items.Count; }
}
}
Now it is ready to be used. Added the following member variables to MainActivity.cs:
private RecyclerView recyclerView;
private List<string> items = new List<string>
{
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"
};Then added the below code in the OnCreate method of MainActivity.cs:
// Find the recycler view by id
this.recyclerView = FindViewById<RecyclerView>(Resource.Id.recycler_view);
// Create an instance of the custom adapter
MyAdapter adapter = new MyAdapter(this.items);
// Set the adapter to the recycler view
recyclerView.SetAdapter(adapter);
// Create an instance of a linear layout manager
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// Set the layout manager to the recycler view
recyclerView.SetLayoutManager(layoutManager);This is all. On running the app on my device I got the below:







