Arthur Kater
Arthur Kater

Reputation: 846

Nested ListView with ItemTemplate selection

Using Xamarin Android with the MvvmCross framework.

I have a list of people, where each person has a nested list of Pets. The people are displayed in a ListView. The nested Pets are displayed in a nested ListView for each person.

How can I assign a custom adapter for each Pets list so the animals can be shown with the itemplate according to the type of animal?

simplified classes:

List<Person> People

class Person
{
    string Name {get; set; }
    bool Sex {get; set; }
    List<IAnimal> Pets {get; set; }
}

class Fish: IAnimal
{
    string Name {get; set; }
    bool HasScales {get; set; }
}

class Bird: IAnimal
{
    string Name {get;set; }
    bool CanFly {get; set; }
    int WingSpan {get; set; }
}

Regards,

Arthur

Upvotes: 1

Views: 1010

Answers (1)

Stuart
Stuart

Reputation: 66882

Putting ListViews within ListViews is often a bad idea from a UX perspective - as the two will scroll independently and consume Touch operations independently. You can if you prefer:

  • use an ExpandableListView (as suggested in @NameSpace's Java answer
  • use an outer ListView where each individual item view contains an inner LinearLayout
  • use a single outer ListView which is bound to a flattened version of your list - e.g. where each Person is a kind of section supplying section start and end rows and where Pets are in the body

How can I assign a custom adapter for each Pets list so the animals can be shown with the itemplate according to the type of animal?

Regardless of which approach you choose to use above, you will need to get hold of the MvxListView or MvxLinearLayout or whatever using FindViewById in its parent - and then you can create and set the Adapter - see an example in https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/Working%20With%20Collections/Collections.Droid/Views/PolymorphicListItemTypesView.cs

If the Adapter parent is itself a list view, then this means you will need to provide a custom MvxListItemView as that parent - e.g. see how a custom view is returned in MvxListView create binding for template layout from code

Upvotes: 1

Related Questions