我有一个ExpandbleListView(从这里复制和粘贴:http://www.appliedcodelog.com/2016/06/expandablelistview-in-xamarin-android.html)
我想知道如何在点击按钮时将子项添加到现有标题中。
到目前为止,我为我的类中的子项而不是在Method中声明了我的List(List lstCS;),在按钮单击处理程序中添加我的Items并调用NotifyDataSetChanged()
但即使它像魅力一样,它也没有那么正确地做到这一点。你有一些东西吗?
using System;
using Android.Widget;
using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Content;
using Android.OS;
using Android.Runtime;
namespace TestApp
{
public class ExpandableListAdapter : BaseExpandableListAdapter
{
private Activity _context;
private List<string> _listDataHeader;
private Dictionary<string, List<string>> _listDataChild;
public ExpandableListAdapter(Activity context, List<string> listDataHeader, Dictionary<String, List<string>> listChildData)
{
_context = context;
_listDataHeader = listDataHeader;
_listDataChild = listChildData;
}
//for cchild item view
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
return _listDataChild[_listDataHeader[groupPosition]][childPosition];
}
public override long GetChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
string childText = (string)GetChild(groupPosition, childPosition);
if (convertView == null)
{
convertView = _context.LayoutInflater.Inflate(Resource.Layout.ListItemCustomLayout, null);
}
TextView txtListChild = (TextView)convertView.FindViewById(Resource.Id.lblListItem);
txtListChild.Text = childText;
return convertView;
}
public override int GetChildrenCount(int groupPosition)
{
return _listDataChild[_listDataHeader[groupPosition]].Count;
}
//For header view
public override Java.Lang.Object GetGroup(int groupPosition)
{
return _listDataHeader[groupPosition];
}
public override int GroupCount
{
get
{
return _listDataHeader.Count;
}
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
string headerTitle = (string)GetGroup(groupPosition);
convertView = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.HeaderCustomLayout, null);
var lblListHeader = (TextView)convertView.FindViewById(Resource.Id.lblListHeader);
lblListHeader.Text = headerTitle;
return convertView;
}
public override bool HasStableIds
{
get
{
return false;
}
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
class ViewHolderItem : Java.Lang.Object
{
}
}
}
using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System;
using System.Runtime.Remoting.Contexts;
namespace TestApp
{
[Activity(Label = "TestApp", MainLauncher = true)]
public class MainActivity : Activity
{
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<string> listDataHeader;
Dictionary<string, List<string>> listDataChild;
int previousGroup = -1;
List<string> lstCS;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
expListView = FindViewById<ExpandableListView>(Resource.Id.lvExp);
// Prepare list data
FnGetListData();
//Bind list
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.SetAdapter(listAdapter);
FnClickEvents();
Button button1 = (Button)FindViewById(Resource.Id.button1);
button1.Click += ButtonClicked;
}
void ButtonClicked(object sender, EventArgs args)
{
lstCS.Add("foo");
listAdapter.NotifyDataSetChanged();
Toast.MakeText(ApplicationContext, "df", ToastLength.Long).Show();
}
void FnClickEvents()
{
//Listening to child item selection
expListView.ChildClick += delegate (object sender, ExpandableListView.ChildClickEventArgs e) {
Toast.MakeText(this, "child clicked", ToastLength.Short).Show();
};
//Listening to group expand
//modified so that on selection of one group other opened group has been closed
expListView.GroupExpand += delegate (object sender, ExpandableListView.GroupExpandEventArgs e) {
if (e.GroupPosition != previousGroup)
expListView.CollapseGroup(previousGroup);
previousGroup = e.GroupPosition;
};
//Listening to group collapse
expListView.GroupCollapse += delegate (object sender, ExpandableListView.GroupCollapseEventArgs e) {
Toast.MakeText(this, "group collapsed", ToastLength.Short).Show();
};
}
void FnGetListData()
{
listDataHeader = new List<string>();
listDataChild = new Dictionary<string, List<string>>();
// Adding child data
listDataHeader.Add("Computer science");
listDataHeader.Add("Electrocs & comm.");
listDataHeader.Add("Mechanical");
// Adding child data
lstCS = new List<string>();
lstCS.Add("Data structure");
lstCS.Add("C# Programming");
lstCS.Add("Java programming");
lstCS.Add("ADA");
lstCS.Add("Operation reserach");
lstCS.Add("OOPS with C");
lstCS.Add("C++ Programming");
var lstEC = new List<string>();
lstEC.Add("Field Theory");
lstEC.Add("Logic Design");
lstEC.Add("Analog electronics");
lstEC.Add("Network analysis");
lstEC.Add("Micro controller");
lstEC.Add("Signals and system");
var lstMech = new List<string>();
lstMech.Add("Instrumentation technology");
lstMech.Add("Dynamics of machinnes");
lstMech.Add("Energy engineering");
lstMech.Add("Design of machine");
lstMech.Add("Turbo machine");
lstMech.Add("Energy conversion");
// Header, Child data
listDataChild.Add(listDataHeader[0], lstCS);
listDataChild.Add(listDataHeader[1], lstEC);
listDataChild.Add(listDataHeader[2], lstMech);
}
}
}
答案 0 :(得分:1)
如果您在自己提供的链接中使用了适配器,则只需在此适配器中添加AddChild
方法,然后通过Activity
进行调用,例如:
public class MyExpandableListAdapter : BaseExpandableListAdapter
{
private Activity _context;
private List<string> _listDataHeader;
private Dictionary<string, List<string>> _listDataChild;
public MyExpandableListAdapter(Activity context, List<string> listDataHeader, Dictionary<string, List<string>> listChildData)
{
_context = context;
_listDataHeader = listDataHeader;
_listDataChild = listChildData;
}
......
public void AddChild(int groupPosition, string newitem)
{
var children = _listDataChild[_listDataHeader[groupPosition]];
children.Add(newitem);
}
}
然后在Activity
中调用它:
Button btn1 = FindViewById<Button>(Resource.Id.ac1btn1);
btn1.Click += (sender, e) =>
{
var parent = listAdapter.GetGroup(0);
listAdapter.AddChild(0, "abc");
listAdapter.NotifyDataSetChanged();
};