2017-02-24 65 views
1

如何在單擊子視圖按鈕後從組中刪除子視圖?xamarin andriod ExpandableListView:從組中刪除或隱藏子視圖

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; 
    } 
} 

回答

0

您可以使用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); 


     } 
    } 

在我的示例,我只是刪除了孩子的第一個項目,您可以使用int groupPosition, int childPosition來獲得當前您點擊的位置。並將其從您的listData中刪除。請記住,在更改數據集時請致電adapter.NotifyDataSetChanged();

我現在用的是ExpandableListView樣品從github

屏幕截圖:

enter image description here

+0

它的工作,,謝謝回覆 – Esraa

相關問題