gpt4 book ai didi

xamarin.android - Mono for Android 和 BaseExpandableListAdapter 示例

转载 作者:行者123 更新时间:2023-12-02 02:16:10 25 4
gpt4 key购买 nike

有没有人有将 BaseExpandableListAdapter 与 Mono for Android 结合使用的示例。我正在尝试针对我的一个观点来实现这一点,但在寻找彻底的东西时遇到了问题。任何人都可以提供任何示例来说明他们如何使用 Mono for Android 来实现这一点吗?

最佳答案

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

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

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

为了演示这一点,下面是一个示例事件,它将加载一个包含一些数据的可扩展列表:

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

关于xamarin.android - Mono for Android 和 BaseExpandableListAdapter 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10502216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com