Xamarin Android ExpandbleListView 添加 child 项

Xamarin Android ExpandbleListView add child items

我有一个 ExpandbleListView(从这里复制和粘贴:http://www.appliedcodelog.com/2016/06/expandablelistview-in-xamarin-android.html

我想知道如何在单击按钮时将 child 项目添加到我现有的 header。

到目前为止,我在我的 class 中为 child 项目声明了我的列表 (List lstCS;),而不是在方法中,在按钮单击处理程序中添加我的项目并调用 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);
    }
}

}

如果您使用了您提供的 link 中的适配器,您只需在此适配器中添加一个 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();
};

如您所见,我使用 Button 将 child 添加到第一组: