C# xamarin android ExpandableListView:从组中删除或隐藏子视图

C# xamarin android ExpandableListView:从组中删除或隐藏子视图,c#,android,xamarin.android,C#,Android,Xamarin.android,如何在单击子视图的按钮后从组中删除子视图 class ExpandableListAdapter : BaseExpandableListAdapter { private ListView listview; private LinearLayout mainLayout; private View linearLayout; public Activity _context; TextView txtListChild; private

如何在单击子视图的按钮后从组中删除子视图

class ExpandableListAdapter : BaseExpandableListAdapter {

    private ListView listview;
    private LinearLayout mainLayout;
    private View linearLayout;

    public Activity _context;

    TextView txtListChild;

    private List<string> _listDataHeader; // header titles
    private Dictionary<string, List<string>> _listDataChild;

    public ExpandableListAdapter(Activity activity, List<string> listDataHeader, Dictionary<String, List<string>> listChildData) {
        //, Dictionary<String, List<string>> listChildData2
        this._context = activity;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
        // this._listDataChild2 = listChildData2;
    }


    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);
        convertView = null;

        if (convertView == null) {
            convertView = _context.LayoutInflater.Inflate(Resource.Layout.childRowWithButton, null);

            Button no = (Button)convertView.FindViewById(Resource.Id.gono);

            no.Click += delegate{
                // I want hide my child
            };

            txtListChild.Text = childText;
        }
        return convertView;
    }

    public override int GetChildrenCount(int groupPosition) {
        return _listDataChild[_listDataHeader[groupPosition]].Count;
    }

    public override Java.Lang.Object GetGroup(int groupPosition) {
        return _listDataHeader[groupPosition];
    }


    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.Header);

        //string headerTitle2 = (string)GetGroup(groupPosition);
        //var ListHeader2 = (TextView)convertView.FindViewById(Resource.Id.Header2);
        //ListHeader2.Text = headerTitle2;

        lblListHeader.Text = headerTitle;

        return convertView;
    }


    public override int GroupCount{
        get{
            return _listDataHeader.Count;
        }
    }


    public override bool HasStableIds{
        get{
            return false;
        }
    }


    public override bool IsChildSelectable(int groupPosition, int childPosition){
        return true;
    }
}
class ExpandableListAdapter:BaseExpandableListAdapter{
私有列表视图列表视图;
私人线路布局主要布局;
私人视图线性布局;
公共活动环境;
文本视图txtListChild;
私有列表_listDataHeader;//头标题
私人字典(listDataChild),;
公共ExpandableListAdapter(活动活动、列表listDataHeader、字典listChildData){
//,字典列表ChildData2
这是上下文=活动;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
//这是._listDataChild2=listChildData2;
}
public override Java.Lang.Object GetChild(int-groupPosition,int-childPosition){
返回_listDataChild[_listDataHeader[groupPosition]][childPosition];
}
公共覆盖长GetChildId(int-groupPosition,int-childPosition){
返回子位置;
}
公共覆盖视图GetChildView(int-groupPosition、int-childPosition、bool-isLastChild、视图转换视图、视图组父视图){
string childText=(string)GetChild(groupPosition,childPosition);
convertView=null;
if(convertView==null){
convertView=_context.LayoutInflater.Inflate(Resource.Layout.childRowWithButton,null);
按钮编号=(按钮)convertView.FindViewById(Resource.Id.gono);
否。单击+=委派{
//我想把我的孩子藏起来
};
Text=childText;
}
返回视图;
}
公共覆盖int GetChildrenCount(int groupPosition){
返回_listDataChild[_listDataHeader[groupPosition]].Count;
}
public override Java.Lang.Object GetGroup(int-groupPosition){
返回_listDataHeader[groupPosition];
}
公共覆盖长GetGroupId(int-groupPosition){
返回组位置;
}
公共覆盖视图GetGroupView(int-groupPosition、bool-isExpanded、视图转换视图、视图组父级){
字符串头文件=(字符串)GetGroup(groupPosition);
convertView=convertView???\u context.LayoutInflater.Inflate(Resource.Layout.HeaderCustomLayout,null);
var lblListHeader=(TextView)convertView.FindViewById(Resource.Id.Header);
//string headerTitle2=(string)GetGroup(groupPosition);
//var ListHeader2=(TextView)convertView.FindViewById(Resource.Id.Header2);
//ListHeader2.Text=headerTitle2;
lblListHeader.Text=标题;
返回视图;
}
公共覆盖int-GroupCount{
得到{
返回_listDataHeader.Count;
}
}
公共覆盖布尔表ID{
得到{
返回false;
}
}
公共覆盖bool ischildselect(int-groupPosition,int-childPosition){
返回true;
}
}

您可以使用
listView.SetOnChildClickListener()
删除子视图,如下代码所示:

public class MainActivity : Activity , IOnChildClickListener
    {
        ExpandableListView listView;
        List<Data> newDataList;
        ExpandableDataAdapter myadapter;
        public bool OnChildClick(ExpandableListView parent, View clickedView, int groupPosition, int childPosition, long id)
        {     
            newDataList.RemoveAt(0);
            myadapter.NotifyDataSetChanged();
            return true;
        }

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.Main);       
            newDataList = Data.SampleData();
            myadapter = new ExpandableDataAdapter(this, newDataList);
            listView = FindViewById<ExpandableListView> (Resource.Id.myExpandableListview);
            listView.SetAdapter (myadapter);
            listView.SetOnChildClickListener(this);


        }
    }
public类main活动:活动,IOnChildClickListener
{
可扩展列表视图列表视图;
列出新的数据列表;
可扩展数据适配器myadapter;
公共bool OnChildClick(可展开列表视图父视图、视图clicked视图、int-groupPosition、int-childPosition、长id)
{     
newDataList.RemoveAt(0);
myadapter.NotifyDataSetChanged();
返回true;
}
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
newDataList=Data.SampleData();
myadapter=新的ExpandableDataAdapter(这是newDataList);
listView=FindViewById(Resource.Id.myExpandableListview);
listView.SetAdapter(myadapter);
SetOnChildClickListener(这个);
}
}
在我的示例中,我只删除了子项的第一项,您可以使用
int-groupPosition,int-childPosition
获取您单击的当前位置。并将其从
列表数据中删除。记住调用
adapter.NotifyDataSetChanged()更改数据集时

我正在使用来自的
ExpandableListView
示例

屏幕截图:


您可以使用
listView.SetOnChildClickListener()
删除子视图,如下代码所示:

public class MainActivity : Activity , IOnChildClickListener
    {
        ExpandableListView listView;
        List<Data> newDataList;
        ExpandableDataAdapter myadapter;
        public bool OnChildClick(ExpandableListView parent, View clickedView, int groupPosition, int childPosition, long id)
        {     
            newDataList.RemoveAt(0);
            myadapter.NotifyDataSetChanged();
            return true;
        }

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.Main);       
            newDataList = Data.SampleData();
            myadapter = new ExpandableDataAdapter(this, newDataList);
            listView = FindViewById<ExpandableListView> (Resource.Id.myExpandableListview);
            listView.SetAdapter (myadapter);
            listView.SetOnChildClickListener(this);


        }
    }
public类main活动:活动,IOnChildClickListener
{
可扩展列表视图列表视图;
列出新的数据列表;
可扩展数据适配器myadapter;
公共bool OnChildClick(可展开列表视图父视图、视图clicked视图、int-groupPosition、int-childPosition、长id)
{     
newDataList.RemoveAt(0);
myadapter.NotifyDataSetChanged();
返回true;
}
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
newDataList=Data.SampleData();
myadapter=新的ExpandableDataAdapter(这是newDataList);
listView=FindViewById(Resource.Id.myExpandableListview);
listView.SetAdapter(myadapter);
SetOnChildClickListener(这个);
}
}
在我的示例中,我只删除了子项的第一项,您可以使用
int-groupPosition,int-childPosition
获取您单击的当前位置。并将其从
列表数据中删除。记住调用
adapter.NotifyDataSetChanged()更改数据集时

我正在使用来自的
ExpandableListView
示例

屏幕截图: