【发布时间】:2014-03-12 19:42:56
【问题描述】:
我写了一个自定义的 ExpandableListView,它的 Header 是一个相对布局,带有 TextView 和 Button 的子级。
在 GetGroupView 中,我定义了一个 btn.Click 委托,它启动了一个新的 Activity:
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 我想启动另一个 Activity:
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 委托被触发两次)。
我在这里错过了什么?
感谢您的帮助!
【问题讨论】:
标签: android click expandablelistview eventhandler