Skip to content

Instantly share code, notes, and snippets.

@benoitjadinon
Last active January 31, 2017 09:34
Show Gist options
  • Save benoitjadinon/144d1e8b56f88ad13b9e to your computer and use it in GitHub Desktop.
Save benoitjadinon/144d1e8b56f88ad13b9e to your computer and use it in GitHub Desktop.
Xamarin.Android FindViewByID that stores a reference to it, for viewholders
public static class ViewExtensions
{
public static T FindViewHolderById<T> (this View @this, int resID)
where T : View
{
return (@this.GetTag (resID) as T) ?? @this.SetTagAndReturnValue (resID, @this.FindViewById<T> (resID)) as T;
}
public static T SetTagAndReturnValue<T> (this T @this, int key, T view)
where T : View
{
@this.SetTag (key, view);
return view;
}
public static T SetTagAndReturnContainer<T> (this T @this, int key, Java.Lang.Object @object)
where T : View
{
@this.SetTag (key, @object);
return @this;
}
}
@benoitjadinon
Copy link
Author

usage : view.FindViewHolderById<TextView> (Resource.Id.text1).Text = "blah";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment