Skip to content

Instantly share code, notes, and snippets.

@ajpinedam
Last active June 14, 2017 16:07
Show Gist options
  • Save ajpinedam/4defac447657adf760640e7fa7759d94 to your computer and use it in GitHub Desktop.
Save ajpinedam/4defac447657adf760640e7fa7759d94 to your computer and use it in GitHub Desktop.
Xamarin.Android Custom Adapter with different layout for Items
using System.Collections.Generic;
using Android;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace GR3_chat_client
{
class customadaptadorburbuja : BaseAdapter
{
public List<Actividadchat.textomasid> textoyid = new List<Actividadchat.textomasid> ();
private const int TYPE_SENT = 0;
private const int TYPE_RECEIVED = 1;
string nombre = "YOUR_USER_NAME";
Context context;
List<string> listillaa = new List<string> ();
public customadaptadorburbuja (Context context, List<Actividadchat.textomasid> listilla, string nombree)
{
this.context = context;
textoyid = listilla;
nombre = nombree;
}
public override Java.Lang.Object GetItem (int position)
{
return textoyid [position].texto;
}
public override long GetItemId (int position)
{
return textoyid [position].id;
}
public override int GetItemViewType (int position)
{
return AmISender(position) ? TYPE_SENT : TYPE_RECEIVED;
}
public override int ViewTypeCount
{
get { return 2; }
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
CustomAdaptadorBurbujaViewHolder holder = null;
var item = textoyid [position];
var username = item.texto.Split ('¹') [0];
var texto = item.texto.Split ('¹') [1];
var viewType = GetItemViewType (position);
var view = convertView;
if (view == null)
{
var inflater = context.GetSystemService (Context.LayoutInflaterService).JavaCast<LayoutInflater> ();
holder = new CustomAdaptadorBurbujaViewHolder();
if (viewType == TYPE_SENT)
view = inflater.Inflate (Resource.Layout.chatsitosderecha, null);
else
view = inflater.Inflate (Resource.Layout.chatsitos, null);
holder.Title = view.FindViewById<TextView> (Resource.Id.textView1);
view.Tag = holder;
}
else
{
holder = view.Tag as CustomAdaptadorBurbujaViewHolder;
}
holder.Title.Text = viewType == TYPE_SENT ? $"Yo: {texto}" : $"${username}: {texto}";
return view;
}
private bool AmISender (int position)
{
var item = textoyid [position];
var username = item.texto.Split ('¹') [0];
return string.Equals (username, nombre, System.StringComparison.InvariantCultureIgnoreCase);
}
public override int Count
{
get { return textoyid.Count; }
}
}
class CustomAdaptadorBurbujaViewHolder : Java.Lang.Object
{
public TextView Title { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment