BackPress在我的适配器中使用buttonclick事件时的问题
问题描述:
我正在使用适配器视图和内部适配器来打开另一个activityB的按钮单击事件。当我在我的活动B中按下后退按钮时,它将重新打开活动B七次,然后返回到我以前的活动A,在那里存在我的适配器视图。它就像在适配器视图中自动运行按钮单击事件我正在按返回按钮。BackPress在我的适配器中使用buttonclick事件时的问题
这是我的适配器代码:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From(mContext).Inflate(Resource.Layout.InventoryPreview, null, false);
}
TextView txtInventoryID = row.FindViewById<TextView>(Resource.Id.txtInventoryID);
txtInventoryID.Text = mitems[position].InventoryItemID;
TextView txtInventoryName = row.FindViewById<TextView>(Resource.Id.txtInventoryName);
txtInventoryName.Text = mitems[position].InventoryItemName;
TextView txtInventoryPrice = row.FindViewById<TextView>(Resource.Id.txtInventoryPrice);
txtInventoryPrice.Text = mitems[position].InventoryItemPrice.Replace(",", ".");
Button ExtraBtn = row.FindViewById<Button>(Resource.Id.ExtrasBtn);
ExtraBtn.Click += (sender, e) =>
{
try
{
mContext.StartActivity(typeof(ExtrasPreviewMain));//Here I open ActivityB.
}
catch (Exception ex)
{
Toast toast = Toast.MakeText(mContext, Convert.ToString(ex), ToastLength.Long);
toast.Show();
}
};
return row;
}
这里是我ActivityB返回按:
public override void OnBackPressed()
{
base.OnBackPressed();
OverridePendingTransition(Resource.Layout.trans_right_in, Resource.Layout.trans_right_out);
}
而且,我在我的XML文件中禁用自动对焦。这是为什么发生?
答
这里的问题是,每次调用GetView
事件处理程序为您的按钮Clicked
事件分配。你似乎从来没有取消它。
所以解决这个问题的一种方法是只分配点击处理程序,如果row == null
。
另一种方法是不要有按钮,而是使用ListView上的ItemSelected
事件。