我写了一个自定义的 ExpandableListView,它的 Header 是一个具有 TextView 和 Button 子的相对布局。
在 GetGroupView 我定义了一个 btn.Click 委托,它启动一个新的活动:
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
var view = convertView;
var group = this.groups[groupPosition];
if (view == null)
{
var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
view = inflater.Inflate(Resource.Layout.ExpListViewItem, null);
}
view.FindViewById<TextView>(Resource.Id.expListViewItem_txt).Text = group.GetHeader();
view.FindViewById<Button>(Resource.Id.expListViewItem_btn).Focusable = false;
view.FindViewById<Button>(Resource.Id.expListViewItem_btn).Click += (object sender, EventArgs e) =>
{
var type = e.GetType();
if(sender.GetType() == typeof(Button))
{
Intent temp = new Intent(context,typeof(TestActivity));
temp.PutExtra("TestValue", group.GetHeader());
context.StartActivity(typeof(TestActivity));
}
};
return view;
}
如果我点击一个 ChildItem 我想开始另一个活动:
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
var view = convertView;
var group = this.groups[groupPosition];
var item = group.GetItems()[childPosition];
if (view == null)
{
var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
view = inflater.Inflate(Resource.Layout.expListViewChildren, null);
}
view.FindViewById<TextView>(Resource.Id.expListViewChildren_txt).Text = item.GetHeader();
view.Click += delegate
{
Intent temp = new Intent(context, typeof(test));
temp.PutExtra("TestValue", item.GetHeader());
context.StartActivity(temp);
};
return view;
}
如果 ListView 折叠,一切都很好,但如果它展开,则 Activity 启动两次(由于 btn.click 委托被触发两次)。
我在这里想念什么?
谢谢你的帮助!