我的回收卡中有一个按钮,需要显示popupMenu
但是,只要用户点击此按钮,就不会显示popupMenu
。即使我在menuOptionsButton.Click
动作中设置了一个断点,它也没有击中。
XML中的ImageButton
<ImageButton
android:id="@+id/MoreSettings"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:src="@drawable/ic_more_black_24dp" />
RecyclerViewAdapter.cs
public class RecyclerAdapter : MvxRecyclerAdapter
{
private readonly FragmentActivity _activity;
public RecyclerAdapter(IMvxAndroidBindingContext bindingContext, FragmentActivity activity)
: base(bindingContext)
{
_activity = activity;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);
var view = holder.ItemView;
var menuOptionsButton = view.FindViewById<ImageButton>(Resource.Id.MoreSettings);
menuOptionsButton.Click += (s, e) =>
{
var popup = new PopupMenu(_activity, menuOptionsButton );
popup.MenuInflater.Inflate(Resource.Menu.menu,popup.Menu);
popup.Show();
};
}
}
如果我没有将popupMenu
放入点击操作中,则recyclerview中的每个项目都会显示popupMenu
,但我希望点击操作以显示popUpMenu
。
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);
var view = holder.ItemView;
var menuOptionsButton = view.FindViewById<ImageButton>(Resource.Id.MoreSettings);
var popup = new PopupMenu(_activity, menuOptionsButton );
popup.MenuInflater.Inflate(Resource.Menu.menu,popup.Menu);
popup.Show();
}
更新:
Activity.cs
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.AView, null);
HasOptionsMenu = true;
toolbar = ((MainView)Activity).SupportActionBar;
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.ARecyclerView);
if (recyclerView != null)
{
recyclerView.HasFixedSize = true;
recyclerView.Adapter = new RecyclerAdapter((IMvxAndroidBindingContext)BindingContext, Activity);
var layoutManager = new LinearLayoutManager(Activity);
recyclerView.SetLayoutManager(layoutManager);
}
return view;
}
答案 0 :(得分:0)
在RecyclerView.Adapter代码中,在OnBindViewHolder方法中添加Click委托时应该小心。如果您添加为C#匿名函数,则每次View都显示新的委托时,将添加,而不是设置。
首先,您应该将此行放在ViewHolder构造函数方法中:
this.menuOptionsButton = view.FindViewById<ImageButton>(Resource.Id.MoreSettings);
然后,
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);
var optionsButton = ((YourHolder)holder).menuOptionsButton;
// This avoids adding a Click event more than once
optionsButton.Click -= this.PopupShow;
optionsButton.Click += this.PopupShow;
}
private void PopupShow(object sender, EventArgs e)
{
Toast.MakeText(this.context, "Button clicked", ToastLength.Short).Show();
var popup = new PopupMenu(_activity, menuOptionsButton );
popup.MenuInflater.Inflate(Resource.Menu.menu,popup.Menu);
popup.Show();
}