Can't set CATiledLayer.FadeDuration in MonoTouch - ios

I'm trying to remove fading animation from CATiledLayer.
Setting CATiledLayer.FadeDuration static property doesn't work and is not supposed to, according to this closed bug report: https://bugzilla.novell.com/show_bug.cgi?id=648993
The suggested solution in Objective-C is to subclass CATiledLayer: How to change iphone CATiledLayer fadeDuration?. I've implemented this in MonoTouch but nothing is being drawn in the view, although DrawInContext is called as expected.
The complete code to reproduce the problem is below. As soon as I remove [Export("fadeDuration")] everything works (but with animation).
Thanks in advance for any help.
using System;
using System.Drawing;
using MonoTouch.CoreAnimation;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;
[Register("NoFadeTiledLayer")]
public class NoFadeTiledLayer : CATiledLayer {
public NoFadeTiledLayer () : base() {}
public NoFadeTiledLayer (IntPtr handle) : base(handle) {}
public override void DrawInContext (MonoTouch.CoreGraphics.CGContext ctx) {
// This is being called everytime
base.DrawInContext (ctx);
}
[Export("fadeDuration")]
public static new double FadeDuration () {
return 0;
}
}
public class ContentView : UIView {
[Export("layerClass")]
public static Class LayerClass () {
return new Class (typeof(NoFadeTiledLayer));
}
public override void Draw (RectangleF rect) {
DrawString ("Lorem ipsum", rect, UIFont.SystemFontOfSize (15));
}
}
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate {
static void Main (string[] args) {
UIApplication.Main (args, null, "AppDelegate");
}
public override bool FinishedLaunching (UIApplication app, NSDictionary options) {
var window = new UIWindow (UIScreen.MainScreen.ApplicationFrame);
window.BackgroundColor = UIColor.Green;
var view = new ContentView ();
view.BackgroundColor = UIColor.White;
view.Frame = window.Bounds;
window.AddSubview (view);
window.MakeKeyAndVisible ();
return true;
}
}

You aren't keeping a reference to your UIWindow, meaning it can be collected and released.
I rewrote your class like so:
using System;
using System.Drawing;
using MonoTouch.CoreAnimation;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;
namespace FadeTest
{
[Register("NoFadeTiledLayer")]
public class NoFadeTiledLayer : CATiledLayer
{
public NoFadeTiledLayer () : base()
{
}
public NoFadeTiledLayer (IntPtr handle) : base(handle)
{
}
public override void DrawInContext (MonoTouch.CoreGraphics.CGContext ctx)
{
// This is being called everytime
base.DrawInContext (ctx);
}
[Export("fadeDuration")]
public static new double FadeDuration
{
get
{
return 0;
}
}
}
public class ContentView : UIView
{
[Export("layerClass")]
public static Class LayerClass ()
{
return new Class (typeof(NoFadeTiledLayer));
}
public override void Draw (RectangleF rect)
{
DrawString ("Lorem ipsum", rect, UIFont.SystemFontOfSize (15));
}
}
public partial class AppDelegate : UIApplicationDelegate
{
static void Main (string[] args)
{
UIApplication.Main (args, null, "AppDelegate");
}
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.ApplicationFrame);
window.BackgroundColor = UIColor.Green;
var view = new ContentView ();
view.BackgroundColor = UIColor.White;
view.Frame = window.Bounds;
window.AddSubview (view);
window.MakeKeyAndVisible ();
return true;
}
}
}
and placed it in the default project template for a window based project, and it worked as expected.

Related

ShouldChangeTextInRange is not called for UITextView

I user custom UITextView and need to hide keyboard on return click. I need to catch 'ShouldChangeTextInRange' what has textview, I don't know why but
method is not called. here is code of for my text view :
public class PlaceholderTextView : UITextView
{
public PlaceholderTextView ()
{
Initialize ();
}
public PlaceholderTextView (CGRect frame)
: base (frame)
{
Initialize ();
}
public PlaceholderTextView (IntPtr handle)
: base (handle)
{
Initialize ();
}
void Initialize ()
{
Text = Placeholder;
ShouldBeginEditing = t => {
if (Text == Placeholder)
Text = string.Empty;
return true;
};
ShouldEndEditing = t => {
if (string.IsNullOrEmpty (Text))
Text = Placeholder;
return true;
};
}
public override bool ShouldChangeTextInRange (UITextRange inRange, string replacementText)
{
if (Text.Equals ("\n")) {
this.EndEditing (true);
return false;
} else {
return true;
}
}
}
In your UITextView subclass, add IUITextViewDelegate and implement ShouldChangeText (selector = textView:shouldChangeTextInRange:replacementText:):
public class MyTextView : UITextView, IUITextViewDelegate
{
string Placeholder;
public MyTextView()
{
Initialize();
}
public MyTextView(Foundation.NSCoder coder) : base(coder)
{
Initialize();
}
public MyTextView(Foundation.NSObjectFlag t) : base(t)
{
Initialize();
}
public MyTextView(IntPtr handle) : base(handle)
{
Initialize();
}
public MyTextView(CoreGraphics.CGRect frame) : base(frame)
{
Initialize();
}
public MyTextView(CoreGraphics.CGRect frame, NSTextContainer textContainer) : base(frame, textContainer)
{
Initialize();
}
void Initialize()
{
Delegate = this;
}
[Export("textViewShouldBeginEditing:")]
public bool ShouldBeginEditing(UITextView textView)
{
if (Text == Placeholder)
Text = string.Empty;
return true;
}
[Export("textViewShouldEndEditing:")]
public bool ShouldEndEditing(UITextView textView)
{
if (string.IsNullOrEmpty(Text))
Text = Placeholder;
return true;
}
[Export("textView:shouldChangeTextInRange:replacementText:")]
public bool ShouldChangeText(UITextView textView, NSRange range, string text)
{
if (text.Contains("\n"))
{
this.EndEditing(true);
return false;
}
return true;
}
}
Note: You can not mix using ObjC/Swift style Delegates and C# anonymous Delegates, otherwise you end up with the error:
Event registration is overwriting existing delegate. Either just use events or your own delegate

Dynamically created Xamarin iOS UI buttons not clickable

I have a Tag class inflated from a nib file:
using Foundation;
using System;
using UIKit;
using ObjCRuntime;
namespace TagTest
{
public partial class Tag : UIView
{
public Tag (IntPtr handle) : base (handle)
{
}
public static Tag Create()
{
var arr = NSBundle.MainBundle.LoadNib("Tag", null, null);
var v = Runtime.GetNSObject<Tag>(arr.ValueAt(0));
return v;
}
public override void AwakeFromNib()
{
}
public UIButton Hashtag
{
get
{
return HashtagBtn;
}
}
public UILabel HashtagCount
{
get
{
return HashtagCountLbl;
}
}
}
}
which is used by the following viewmodel
using System.Collections.Generic;
using MvvmCross.Binding.BindingContext;
using MvvmCross.iOS.Views;
using TagTest.Core.ViewModels;
using UIKit;
using Cirrious.FluentLayouts.Touch;
using System;
using System.Drawing;
namespace TagTest
{
public partial class SearchView : MvxViewController
{
List<Tag> _tags;
public SearchView () : base ("SearchView", null)
{
}
new SearchViewModel ViewModel
{
get
{
return (SearchViewModel)base.ViewModel;
}
set
{
base.ViewModel = value;
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
DisplayTags();
View.UserInteractionEnabled = false;
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.TranslatesAutoresizingMaskIntoConstraints = false;
EdgesForExtendedLayout = UIRectEdge.None;
ExtendedLayoutIncludesOpaqueBars = false;
AutomaticallyAdjustsScrollViewInsets = false;
View.ClipsToBounds = true;
View.BackgroundColor = UIColor.Red;
CreateBindings ();
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
ViewModel.Init();
}
void CreateBindings()
{
var set = this.CreateBindingSet<SearchView, SearchViewModel> ();
set.Bind (this).For(x => x.Title).To (vm => vm.Title);
set.Apply ();
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
View.LayoutIfNeeded();
}
void DisplayTags()
{
_tags = new List<Tag>();
if (ViewModel.Tags != null)
{
foreach (var item in ViewModel.Tags)
{
_tags.Add(Tag.Create());
}
UIView lastTemplateAdded = View;
for (int i = 0; i < _tags.Count; i++)
{
var tag = _tags[i];
tag.TranslatesAutoresizingMaskIntoConstraints = false;
tag.Hashtag.SetTitle(ViewModel.Tags[i].Tagname, UIControlState.Normal);
tag.HashtagCount.Text = ViewModel.Tags[i].Count.ToString();
tag.Frame = new RectangleF(100f, 300f, 100f, 50f);
tag.Hashtag.Enabled = true;
tag.Hashtag.UserInteractionEnabled = true;
tag.UserInteractionEnabled = true;
tag.Hashtag.TouchUpInside += (sender, e) =>
{
ViewModel.TagSelectedCommand.Execute(tag);
};
View.AddSubview(tag);
if (i == 0)
{
View.AddConstraints(
tag.AtTopOf(View),
tag.AtLeftOf(View),
tag.Height().EqualTo(20)
);
}
else
{
View.AddConstraints(
tag.Below(lastTemplateAdded, 20),
tag.AtLeftOf(View),
tag.Height().EqualTo(20)
);
}
lastTemplateAdded = tag;
}
}
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
}
}
but the Hashtag button is not clickable, the TouchupInside doesn't appear to be fired. If I add a single button to the view it is clickable. What could be going wrong?
Found the problem. It was a combination of setting View.UserInteractionEnabled = false; and using the fluent constraints. I've now set UserInteractioEnabled to true and removed the fluent constraints. All working now.

How do I keep the keyboard from covering my UI instead of resizing it?

In iOS, Xamarin.Forms resizes the screen when the keyboard comes up when the root node is a ScrollView. But when the root node is not a ScrollView the keyboard hides part of the UI. How do you keep this from happening?
The way to fix this is with a custom renderer that listens for the keyboard showing up and adds padding while it's there.
In your PCL project, KeyboardResizingAwareContentPage.cs:
using Xamarin.Forms;
public class KeyboardResizingAwareContentPage : ContentPage {
public bool CancelsTouchesInView = true;
}
In your iOS project, IosKeyboardFixPageRenderer.cs:
using Foundation;
using MyProject.iOS.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(KeyboardResizingAwareContentPage), typeof(IosKeyboardFixPageRenderer))]
namespace MyProject.iOS.Renderers {
public class IosKeyboardFixPageRenderer : PageRenderer {
NSObject observerHideKeyboard;
NSObject observerShowKeyboard;
public override void ViewDidLoad()
{
base.ViewDidLoad();
var cp = Element as KeyboardResizingAwareContentPage;
if (cp != null && !cp.CancelsTouchesInView) {
foreach (var g in View.GestureRecognizers) {
g.CancelsTouchesInView = false;
}
}
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
observerHideKeyboard = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
observerShowKeyboard = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
NSNotificationCenter.DefaultCenter.RemoveObserver(observerHideKeyboard);
NSNotificationCenter.DefaultCenter.RemoveObserver(observerShowKeyboard);
}
void OnKeyboardNotification(NSNotification notification)
{
if (!IsViewLoaded) return;
var frameBegin = UIKeyboard.FrameBeginFromNotification(notification);
var frameEnd = UIKeyboard.FrameEndFromNotification(notification);
var page = Element as ContentPage;
if (page != null && !(page.Content is ScrollView)) {
var padding = page.Padding;
page.Padding = new Thickness(padding.Left, padding.Top, padding.Right, padding.Bottom + frameBegin.Top - frameEnd.Top);
}
}
}
}
I found that the KeyboardOverlap plugin works better than the solution of Anthony.
This is how I use it:
Create a custom renderer
public class KeyboardResizingAwareContentPage : ContentPage
{
}
Implement the custom renderer on iOS. Here is the important part of paulpatarinski code:
[Preserve (AllMembers = true)]
public class KeyboardOverlapRenderer : PageRenderer
{
NSObject _keyboardShowObserver;
NSObject _keyboardHideObserver;
private bool _pageWasShiftedUp;
private double _activeViewBottom;
private bool _isKeyboardShown;
public static void Init ()
{
var now = DateTime.Now;
Debug.WriteLine ("Keyboard Overlap plugin initialized {0}", now);
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
var page = Element as ContentPage;
if (page != null) {
var contentScrollView = page.Content as ScrollView;
if (contentScrollView != null)
return;
RegisterForKeyboardNotifications ();
}
}
public override void ViewWillDisappear (bool animated)
{
base.ViewWillDisappear (animated);
UnregisterForKeyboardNotifications ();
}
void RegisterForKeyboardNotifications ()
{
if (_keyboardShowObserver == null)
_keyboardShowObserver = NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillShowNotification, OnKeyboardShow);
if (_keyboardHideObserver == null)
_keyboardHideObserver = NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillHideNotification, OnKeyboardHide);
}
void UnregisterForKeyboardNotifications ()
{
_isKeyboardShown = false;
if (_keyboardShowObserver != null) {
NSNotificationCenter.DefaultCenter.RemoveObserver (_keyboardShowObserver);
_keyboardShowObserver.Dispose ();
_keyboardShowObserver = null;
}
if (_keyboardHideObserver != null) {
NSNotificationCenter.DefaultCenter.RemoveObserver (_keyboardHideObserver);
_keyboardHideObserver.Dispose ();
_keyboardHideObserver = null;
}
}
protected virtual void OnKeyboardShow (NSNotification notification)
{
if (!IsViewLoaded || _isKeyboardShown)
return;
_isKeyboardShown = true;
var activeView = View.FindFirstResponder ();
if (activeView == null)
return;
var keyboardFrame = UIKeyboard.FrameEndFromNotification (notification);
var isOverlapping = activeView.IsKeyboardOverlapping (View, keyboardFrame);
if (!isOverlapping)
return;
if (isOverlapping) {
_activeViewBottom = activeView.GetViewRelativeBottom (View);
ShiftPageUp (keyboardFrame.Height, _activeViewBottom);
}
}
private void OnKeyboardHide (NSNotification notification)
{
if (!IsViewLoaded)
return;
_isKeyboardShown = false;
var keyboardFrame = UIKeyboard.FrameEndFromNotification (notification);
if (_pageWasShiftedUp) {
ShiftPageDown (keyboardFrame.Height, _activeViewBottom);
}
}
private void ShiftPageUp (nfloat keyboardHeight, double activeViewBottom)
{
var pageFrame = Element.Bounds;
var newY = pageFrame.Y + CalculateShiftByAmount (pageFrame.Height, keyboardHeight, activeViewBottom);
Element.LayoutTo (new Rectangle (pageFrame.X, newY,
pageFrame.Width, pageFrame.Height));
_pageWasShiftedUp = true;
}
private void ShiftPageDown (nfloat keyboardHeight, double activeViewBottom)
{
var pageFrame = Element.Bounds;
var newY = pageFrame.Y - CalculateShiftByAmount (pageFrame.Height, keyboardHeight, activeViewBottom);
Element.LayoutTo (new Rectangle (pageFrame.X, newY,
pageFrame.Width, pageFrame.Height));
_pageWasShiftedUp = false;
}
private double CalculateShiftByAmount (double pageHeight, nfloat keyboardHeight, double activeViewBottom)
{
return (pageHeight - activeViewBottom) - keyboardHeight;
}
}
And the missing extensions:
public static class ViewExtensions
{
/// <summary>
/// Find the first responder in the <paramref name="view"/>'s subview hierarchy
/// </summary>
/// <param name="view">
/// A <see cref="UIView"/>
/// </param>
/// <returns>
/// A <see cref="UIView"/> that is the first responder or null if there is no first responder
/// </returns>
public static UIView FindFirstResponder (this UIView view)
{
if (view.IsFirstResponder) {
return view;
}
foreach (UIView subView in view.Subviews) {
var firstResponder = subView.FindFirstResponder ();
if (firstResponder != null)
return firstResponder;
}
return null;
}
/// <summary>
/// Returns the new view Bottom (Y + Height) coordinates relative to the rootView
/// </summary>
/// <returns>The view relative bottom.</returns>
/// <param name="view">View.</param>
/// <param name="rootView">Root view.</param>
public static double GetViewRelativeBottom (this UIView view, UIView rootView)
{
var viewRelativeCoordinates = rootView.ConvertPointFromView (view.Frame.Location, view);
var activeViewRoundedY = Math.Round (viewRelativeCoordinates.Y, 2);
return activeViewRoundedY + view.Frame.Height;
}
/// <summary>
/// Determines if the UIView is overlapped by the keyboard
/// </summary>
/// <returns><c>true</c> if is keyboard overlapping the specified activeView rootView keyboardFrame; otherwise, <c>false</c>.</returns>
/// <param name="activeView">Active view.</param>
/// <param name="rootView">Root view.</param>
/// <param name="keyboardFrame">Keyboard frame.</param>
public static bool IsKeyboardOverlapping (this UIView activeView, UIView rootView, CGRect keyboardFrame)
{
var activeViewBottom = activeView.GetViewRelativeBottom (rootView);
var pageHeight = rootView.Frame.Height;
var keyboardHeight = keyboardFrame.Height;
var isOverlapping = activeViewBottom >= (pageHeight - keyboardHeight);
return isOverlapping;
}
}
Use the custom page renderer
public partial class LoginPage : KeyboardResizingAwareContentPage
{
public LoginPage()
{
// your content
// note: you have to use base.Navigation.PushAsync(), base.DisplayAlert(), ...
}
}
<?xml version="1.0" encoding="utf-8" ?>
<renderer:KeyboardResizingAwareContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App.Pages.LoginPage"
xmlns:renderer="clr-namespace:App.CustomRenderers;assembly=App">
<!-- your content -->
</renderer:KeyboardResizingAwareContentPage>
All credits for this goes to Paul! Thanks for this!
This Xamarin Forum question discusses it.
In addition if you want it in Android using Xamarin / Forms you can just set this in your main activity:
[Activity(WindowSoftInputMode = Android.Views.SoftInput.AdjustResize)]
public class MainActivity
...
For Android add following code in MainActivity after LoadApplication(new App());
App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().
UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);

Monotouch change UIImageView image from another class

I'm trying to update an imageview (an outlet from Xcode) from another class, but it didn't work.
AppDelegate:
public UIImageView imageviewLoanInputTabs
{
get;
private set;
}
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
imageviewLoanInputTabs = new UIImageView ();
return true;
}
The other class:
var imageObjOutraClass = (AppDelegate) UIApplication.SharedApplication.Delegate;
imageObjOutraClass.imageviewLoanInputTabs.Image = (UIImage.FromBundle ("image11.png"));
There's no special trick to manipulating UIImageViews. Try the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
namespace SingleFileSolution
{
public class ContentView : UIView
{
public ContentView Companion { get; set; }
public UIImage Image {
get
{
return imageView.Image;
}
set
{
imageView.Image = value;
}
}
private UIImageView imageView;
public ContentView(UIColor fillColor, UIImage initialImage)
{
this.BackgroundColor = fillColor;
imageView = new UIImageView(new RectangleF(10, 60, initialImage.Size.Width, initialImage.Size.Height));
imageView.Image = initialImage;
AddSubview(imageView);
UIButton swapButton = UIButton.FromType(UIButtonType.RoundedRect);
swapButton.SetTitle("Swap Image", UIControlState.Normal);
swapButton.TouchUpInside += (sender, e) => {
if(Companion != null)
{
var tmp = Companion.Image;
Companion.Image = this.Image;
this.Image = tmp;
}
};
swapButton.Frame = new RectangleF(10, 10, 100, 44);
AddSubview(swapButton);
}
}
public class SimpleViewController : UIViewController
{
public SimpleViewController() : base ()
{
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
public override void ViewDidLoad()
{
var view1 = new ContentView(UIColor.Blue, UIImage.FromFile("image1.jpg"));
var view2 = new ContentView(UIColor.Red, UIImage.FromFile("image2.jpg"));
view1.Frame = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height / 2);
view2.Frame = new RectangleF(0, UIScreen.MainScreen.Bounds.Height / 2, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height / 2);
view1.Companion = view2;
view2.Companion = view1;
var view = new UIView(new RectangleF(new PointF(0, 0), UIScreen.MainScreen.Bounds.Size));
view.AddSubview(view1);
view.AddSubview(view2);
this.View = view;
}
}
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
UIWindow window;
SimpleViewController svController;
UINavigationController navController;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
window = new UIWindow(UIScreen.MainScreen.Bounds);
svController = new SimpleViewController();
window.RootViewController = svController;
window.MakeKeyAndVisible();
return true;
}
}
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
}
}

Mono for Android and BaseExpandableListAdapter example

Does anyone have an examples for using the BaseExpandableListAdapter with Mono for Android. I'm trying to implement this for one of my views, but am having issues with finding something that is thorough. Can anyone provide any examples on how they got this working with Mono for Android?
Here's an admittedly crude looking but functional example of using a custom expandable list adapter. You can think of the data source as being a list of lists, since each item will expand to display a list of items underneath it. To represent that, we'll use this simple model:
public class Group : Java.Lang.Object
{
public string Name { get; set; }
public IList<string> Items { get; set; }
}
Using that model you can then create a class that inherits from BaseExpandableListAdapter and implements all the required methods/properties:
public class MyAdapter : BaseExpandableListAdapter
{
private readonly Context _context;
private readonly IList<Group> _groups;
public MyAdapter(Context context, IList<Group> groups)
{
_context = context;
_groups = groups;
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
return _groups[groupPosition].Items[childPosition];
}
public override long GetChildId(int groupPosition, int childPosition)
{
return (groupPosition * _groups.Count) + childPosition;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
var view = (TextView)(convertView ?? new TextView(_context));
view.Text = _groups[groupPosition].Items[childPosition];
return view;
}
public override int GetChildrenCount(int groupPosition)
{
return _groups[groupPosition].Items.Count;
}
public override Java.Lang.Object GetGroup(int groupPosition)
{
return _groups[groupPosition];
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
var view = (TextView)(convertView ?? new TextView(_context));
view.Text = _groups[groupPosition].Name;
return view;
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
public override int GroupCount
{
get { return _groups.Count; }
}
public override bool HasStableIds
{
get { return true; }
}
}
The constructor for the adapter takes in a list of groups, and uses that to implement the methods that ask for a group, item, etc. To keep things simple, for each view I'm just rendering a single TextView but you can create/inflate any view you want for the items.
To demonstrate this in action, here's a sample activity that will load up an expandable list with some data:
[Activity(Label = "ExpandableListDemo", MainLauncher = true, Icon = "#drawable/icon")]
public class MyExpandableListActivity : ExpandableListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var groups = new List<Group>
{
new Group
{
Name = "Group 1",
Items = new List<string> { "Item 1.1", "Item 1.2", "Item 1.3" }
},
new Group
{
Name = "Group 2",
Items = new List<string> { "Item 2.1", "Item 2.2", "Item 2.3" }
}
};
var adapter = new MyAdapter(this, groups);
SetListAdapter(adapter);
}
}

Resources