Mono for Android和BaseExpandableListAdapter示例

时间:2012-05-08 16:03:45

标签: xamarin.android

有没有人有一个使用BaseExpandableListAdapter和Mono for Android的例子。我正在尝试为我的一个观点实现这个,但是在找到彻底的问题时遇到了问题。任何人都可以提供任何关于他们如何使用Mono for Android的示例吗?

1 个答案:

答案 0 :(得分:2)

这是一个使用自定义可扩展列表适配器的粗略但功能性的示例。您可以将数据源视为列表列表,因为每个项目都将展开以显示其下方的项目列表。为了表示这一点,我们将使用这个简单的模型:

public class Group : Java.Lang.Object
{
    public string Name { get; set; }
    public IList<string> Items { get; set; }
}

使用该模型,您可以创建一个继承自BaseExpandableListAdapter的类,并实现所有必需的方法/属性:

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; }
    }
}

适配器的构造函数接受一个组列表,并使用它来实现请求组,项目等的方法。为了简单起见,对于每个视图,我只是渲染单个TextView但是你可以为项目创建/扩充您想要的任何视图。

为了证明这一点,这里是一个示例活动,它将加载包含一些数据的可扩展列表:

[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);
    }
}