Reputation: 2701
I'm just starting to look into Android programming with MonoDroid, coming from a Silverlight/WPF/WinForms background.
I'm trying to work out how you would create a ViewGroup that constructs child elements which are constructed based on some form of template. Preferably this would be specifiable in the layout XML. Can anyone point out how you do this?
Are there are any samples that demonstrate this kind of behavior?
I'm looking for behaviour that is similar to the DataTemplate in the SL/WPF/WinRT ItemsControl.
Upvotes: 0
Views: 456
Reputation: 10139
I'm not sure what you're looking to do, but it sounds like you're looking for a way to display a list of items, where each item is based on a particular layout? If that's the case then the ListView class is probably what you're looking for. In Android each item in a list can be any view, and can be created either from code or from a layout defined in XML. Xamarin has some simple list examples available here to help get started.
As you want to customize list items more, you may find yourself wanting to create your own custom list adapter class instead of using one of the built-in ones. That might look something like this, for example:
public class MyListAdapter : BaseAdapter<MyModel>
{
private readonly Activity _context;
private readonly IList<MyModel> _items;
public MyListAdapter(Activity context, IList<MyModel> items)
{
_context = context;
_items = items;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView
?? _context.LayoutInflater.Inflate(Resource.Layout.Item, null);
view.FindViewById<TextView>(Resource.Id.Name).Text = _items[position].Name;
return view;
}
public override int Count
{
get { return _items.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override MyModel this[int position]
{
get { return _items[position]; }
}
}
Upvotes: 1