【发布时间】:2018-10-18 05:18:27
【问题描述】:
如何在 xamarin android 的可扩展列表视图中添加一个或多个级别。 或如何在 xamarin android 中创建树视图。
我找到了单级 Listview 的解决方案。 下面是我的代码。 我对 xamarin android 很陌生。 请帮忙。 提前致谢。
我的 MainActivity.cs:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
using System.Collections.Generic;
using System;
namespace AndroidExpandableListView
{
[Activity(Label = "AndroidExpandableListView", MainLauncher = true, Icon = "@drawable/icon",
Theme ="@style/MyTheme")]
public class MainActivity : AppCompatActivity
{
ExpandableListViewAdapter mAdapter;
ExpandableListView expandableListView;
List<string> group = new List<string>();
Dictionary<string, List<string>> dicMyMap = new Dictionary<string, List<string>>();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.Title = "Expandable ListView";
expandableListView = FindViewById<ExpandableListView>(Resource.Id.expandableListView);
//Set Data
SetData(out mAdapter);
expandableListView.SetAdapter(mAdapter);
expandableListView.ChildClick += (s, e) => {
Toast.MakeText(this, "Clicked : " + mAdapter.GetChild(e.GroupPosition, e.ChildPosition), ToastLength.Short).Show();
};
}
private void SetData(out ExpandableListViewAdapter mAdapter)
{
List<string> groupA = new List<string>();
groupA.Add("A-1");
groupA.Add("A-2");
groupA.Add("A-3");
List<string> groupB = new List<string>();
groupB.Add("B-1");
groupB.Add("B-2");
groupB.Add("B-3");
group.Add("Group A");
group.Add("Group B");
dicMyMap.Add(group[0], groupA);
dicMyMap.Add(group[1], groupB);
mAdapter = new ExpandableListViewAdapter(this, group, dicMyMap);
}
}
}
我的 ExpandableListViewAdapter.cs:
namespace AndroidExpandableListView
{
public class ExpandableListViewAdapter : BaseExpandableListAdapter
{
private Context context;
private List<string> listGroup;
private Dictionary<string, Dictionary<string, List<string>>> lstChild;
public ExpandableListViewAdapter(Context context, List<string> listGroup, Dictionary<string, Dictionary<string, List<string>>> lstChild)
{
this.context = context;
this.listGroup = listGroup;
this.lstChild = lstChild;
}
public override int GroupCount
{
get
{
return listGroup.Count;
}
}
public override bool HasStableIds
{
get
{
return false;
}
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
var result = new List<string>();
lstChild.TryGetValue(listGroup[groupPosition], out result);
return 0;// result[childPosition];
}
public override long GetChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public override int GetChildrenCount(int groupPosition)
{
var result = new Dictionary<string, List<string>>();
lstChild.TryGetValue(listGroup[groupPosition], out result);
return result.Count;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.item_layout, null);
}
TextView textViewItem = convertView.FindViewById<TextView>(Resource.Id.item);
string content = (string)GetChild(groupPosition, childPosition);
textViewItem.Text = content;
return convertView;
}
public override Java.Lang.Object GetGroup(int groupPosition)
{
return listGroup[groupPosition];
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.group_item, null);
}
string textGroup = (string)GetGroup(groupPosition);
TextView textViewGroup = convertView.FindViewById<TextView>(Resource.Id.group);
textViewGroup.Text = textGroup;
return convertView;
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
}
我想在 xamarin android 中开发如下图所示的 Listview。我不确定它是否可以使用 Expandable Listview 实现,因为我尝试过但似乎没有任何效果。任何链接都会有所帮助
【问题讨论】:
-
你能展示你想要达到的目标吗?
-
更新了我的问题
标签: listview xamarin.android expandablelistview