Last active
January 31, 2017 09:34
-
-
Save benoitjadinon/144d1e8b56f88ad13b9e to your computer and use it in GitHub Desktop.
Xamarin.Android FindViewByID that stores a reference to it, for viewholders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage :
view.FindViewHolderById<TextView> (Resource.Id.text1).Text = "blah";