diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs b/Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs new file mode 100644 index 00000000..7855b01e --- /dev/null +++ b/Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs @@ -0,0 +1,208 @@ +using System.IO; +using System.Threading.Tasks; +using Android.Content; +using Android.Graphics; +using Android.Text; +using Android.Views; +using Android.Widget; +using AView = Android.Views.View; +using AColor = Android.Graphics.Color; +using AColorDraw = Android.Graphics.Drawables.ColorDrawable; + +namespace Xamarin.Forms.Platform.Android +{ + public class BaseCellView : LinearLayout, INativeElementView + { + public const double DefaultMinHeight = 44; + + readonly Color _androidDefaultTextColor; + readonly Cell _cell; + readonly TextView _detailText; + readonly ImageView _imageView; + readonly TextView _mainText; + Color _defaultDetailColor; + Color _defaultMainTextColor; + Color _detailTextColor; + string _detailTextText; + ImageSource _imageSource; + Color _mainTextColor; + string _mainTextText; + + public BaseCellView(Context context, Cell cell) : base(context) + { + _cell = cell; + SetMinimumWidth((int)context.ToPixels(25)); + SetMinimumHeight((int)context.ToPixels(25)); + Orientation = Orientation.Horizontal; + + var padding = (int)context.FromPixels(8); + SetPadding(padding, padding, padding, padding); + + _imageView = new ImageView(context); + var imageParams = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.FillParent) + { + Width = (int)context.ToPixels(60), + Height = (int)context.ToPixels(60), + RightMargin = 0, + Gravity = GravityFlags.Center + }; + using(imageParams) + AddView(_imageView, imageParams); + + var textLayout = new LinearLayout(context) { Orientation = Orientation.Vertical }; + + _mainText = new TextView(context); + _mainText.SetSingleLine(true); + _mainText.Ellipsize = TextUtils.TruncateAt.End; + _mainText.SetPadding((int)context.ToPixels(15), padding, padding, padding); + _mainText.SetTextAppearance(context, global::Android.Resource.Attribute.TextAppearanceListItem); + using(var lp = new LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent)) + textLayout.AddView(_mainText, lp); + + _detailText = new TextView(context); + _detailText.SetSingleLine(true); + _detailText.Ellipsize = TextUtils.TruncateAt.End; + _detailText.SetPadding((int)context.ToPixels(15), padding, padding, padding); + _detailText.Visibility = ViewStates.Gone; + _detailText.SetTextAppearance(context, global::Android.Resource.Attribute.TextAppearanceListItemSmall); + using(var lp = new LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent)) + textLayout.AddView(_detailText, lp); + + var layoutParams = new LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent) { Width = 0, Weight = 1, Gravity = GravityFlags.Center }; + + using(layoutParams) + AddView(textLayout, layoutParams); + + SetMinimumHeight((int)context.ToPixels(DefaultMinHeight)); + _androidDefaultTextColor = Color.FromUint((uint)_mainText.CurrentTextColor); + } + + public AView AccessoryView { get; private set; } + + public string DetailText + { + get { return _detailTextText; } + set + { + if (_detailTextText == value) + return; + + _detailTextText = value; + _detailText.Text = value; + _detailText.Visibility = string.IsNullOrEmpty(value) ? ViewStates.Gone : ViewStates.Visible; + } + } + + public string MainText + { + get { return _mainTextText; } + set + { + if (_mainTextText == value) + return; + + _mainTextText = value; + _mainText.Text = value; + } + } + + Element INativeElementView.Element + { + get { return _cell; } + } + + public void SetAccessoryView(AView view) + { + if (AccessoryView != null) + RemoveView(AccessoryView); + + if (view != null) + { + using(var layout = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.FillParent)) + AddView(view, layout); + + AccessoryView = view; + } + } + + public void SetDefaultMainTextColor(Color defaultColor) + { + _defaultMainTextColor = defaultColor; + if (_mainTextColor == Color.Default) + _mainText.SetTextColor(defaultColor.ToAndroid()); + } + + public void SetDetailTextColor(Color color) + { + if (_detailTextColor == color) + return; + + if (_defaultDetailColor == Color.Default) + _defaultDetailColor = Color.FromUint((uint)_detailText.CurrentTextColor); + + _detailTextColor = color; + _detailText.SetTextColor(color.ToAndroid(_defaultDetailColor)); + } + + public void SetImageSource(ImageSource source) + { + UpdateBitmap(source, _imageSource); + _imageSource = source; + } + + public void SetImageVisible(bool visible) + { + _imageView.Visibility = visible ? ViewStates.Visible : ViewStates.Gone; + } + + public void SetIsEnabled(bool isEnable) + { + _mainText.Enabled = isEnable; + _detailText.Enabled = isEnable; + } + + public void SetMainTextColor(Color color) + { + Color defaultColorToSet = _defaultMainTextColor == Color.Default ? _androidDefaultTextColor : _defaultMainTextColor; + + _mainTextColor = color; + _mainText.SetTextColor(color.ToAndroid(defaultColorToSet)); + } + + public void SetRenderHeight(double height) + { + height = Context.ToPixels(height); + LayoutParameters = new LayoutParams(ViewGroup.LayoutParams.MatchParent, (int)(height == -1 ? ViewGroup.LayoutParams.WrapContent : height)); + } + + async void UpdateBitmap(ImageSource source, ImageSource previousSource = null) + { + if (Equals(source, previousSource)) + return; + + _imageView.SetImageResource(global::Android.Resource.Color.Transparent); + + Bitmap bitmap = null; + + IImageSourceHandler handler; + + if (source != null && (handler = Registrar.Registered.GetHandler<IImageSourceHandler>(source.GetType())) != null) + { + try + { + bitmap = await handler.LoadImageAsync(source, Context); + } + catch (TaskCanceledException) + { + } + catch (IOException e) + { + } + } + + _imageView.SetImageBitmap(bitmap); + if (bitmap != null) + bitmap.Dispose(); + } + } +}
\ No newline at end of file |