1
我有一個ExpandbleListView(複製從這裏&貼:http://www.appliedcodelog.com/2016/06/expandablelistview-in-xamarin-android.html)Xamarin的Android ExpandbleListView添加子項
我想知道我怎麼可以點擊按鈕添加子項以我現有的頭。
到目前爲止,我宣佈我的名單(名單lstCS),表示在我的課,而不是在方法子項,加上我項目在按鈕單擊處理程序,並調用NotifyDataSetChanged()
,但它只是沒有感覺正確地做到這一點,即使它像魅力一樣工作。你有一些tipps?
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);
}
}
}