Skip to content

Instantly share code, notes, and snippets.

@jpda jpda/Contact.cs
Last active Aug 9, 2018

Embed
What would you like to do?
Xamarin.Android RecylerView adapter
public class Contact
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
}
public class ContactsAdapterViewHolder : RecyclerView.ViewHolder
{
public TextView Name { get; private set; }
public TextView Email { get; private set; }
public ContactAdapterViewHolder(View itemView) : base(itemView)
{
Name = itemView.FindViewById<TextView>(Resource.Id.Name);
Email = itemView.FindViewById<TextView>(Resource.Id.Email);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/Thumbnail"
android:layout_width="70dp"
android:layout_height="70dp"
android:paddingLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/contactplaceholder" />
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toRightOf="@id/Thumbnail"
android:lineSpacingExtra="3dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#333"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/Name"
android:layout_toRightOf="@id/Thumbnail"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#333"
android:textSize="11sp" />
</RelativeLayout>
public class ContactsAdapter : RecyclerView.Adapter
{
private readonly Activity _context;
private readonly List<Contact> _contacts;
public ListAdapter(Activity context, List<Contact> contacts) : base()
{
_context = context;
_contacts = contacts;
}
public override long GetItemId(int position)
{
return position;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ContactListRow, parent, false);
var viewHolder = new ContactsAdapterViewHolder();
return viewHolder;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var item = _list[position];
var h = (ContactAdapterViewHolder)holder;
h.Email.Text = item.Email;
h.Name.Text = item.Name;
}
public override int ItemCount => _list.Count;
}
public class ListAdapter<T, V> : RecyclerView.Adapter where V : RecyclerView.ViewHolder
{
// where T is the type of object in the collection
// V is your ViewHolder
private readonly Activity _context;
private readonly List<T> _list;
private readonly Action<T, V> _binder;
private readonly int _viewLayoutId;
public ListAdapter(Activity context, List<T> list, Action<T, V> binder, int viewLayoutId) : base()
{
_context = context;
_list = list;
_binder = binder;
_viewLayoutId = viewLayoutId;
}
public override long GetItemId(int position)
{
return position;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(_viewLayoutId, parent, false);
var viewHolder = Activator.CreateInstance(typeof(V), new object[] { itemView }) as V;
return viewHolder;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var item = _list[position];
var h = holder as V;
_binder(item, h);
}
public override int ItemCount => _list.Count;
}
var adapter = new ListAdapter<Contact, ContactsAdapterViewHolder>(this, result, (thing, holder) =>
{
holder.Name.Text = thing.Name;
holder.Email.Text = thing.Email;
}, Resource.Layout.ContactListRow);
view.SetAdapter(adapter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.