自定义组件中的上下文菜单
我正在构建一个自定义组件,我想在其中添加上下文菜单。到目前为止,我已经成功创建并显示了上下文菜单,执行以下操作:
public class CustomComponent extends LinearLayout implements OnClickListener, OnCreateContextMenuListener {
private final MenuInflater menuInflator;
public CustomComponent(final Context context) {
this(context, null);
}
public CustomComponent(final Context context, final AttributeSet attrs) {
super(context, attrs);
menuInflator = new MenuInflater(context);
final LayoutInflater inf = LayoutInflater.from(context);
inf.inflate(R.layout.component, this, true);
setOnClickListener(this);
setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menuInflator.inflate(R.menu.menu_context, menu);
}
@Override
public void onClick(View v) {
showContextMenu();
}
}
我现在面临的问题是我不知道如何在该组件内调用 onContextItemSelected() 函数。我正在查看 Activity 中的一些源代码,注意到它有一个 mWindow 成员来保存这些回调。我不太确定这种方式是否可行。有什么建议吗?
I'm building my a custom component where i want to have a context menu on. So far i've succesfully created and shown the context menu doing the following:
public class CustomComponent extends LinearLayout implements OnClickListener, OnCreateContextMenuListener {
private final MenuInflater menuInflator;
public CustomComponent(final Context context) {
this(context, null);
}
public CustomComponent(final Context context, final AttributeSet attrs) {
super(context, attrs);
menuInflator = new MenuInflater(context);
final LayoutInflater inf = LayoutInflater.from(context);
inf.inflate(R.layout.component, this, true);
setOnClickListener(this);
setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menuInflator.inflate(R.menu.menu_context, menu);
}
@Override
public void onClick(View v) {
showContextMenu();
}
}
The problem i'm facing now is that i don't know how to get the onContextItemSelected() function to be called inside this component. I was looking through some source in Activity and noticed that it has a mWindow member that holds these callbacks. I'm not really sure if it is possible to it this way. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在搜索其他选项后,我发现了一个非常简单的解决方案:
当然,您需要实现 OnMenuItemClickListener 才能使其工作
Well after searching for other options i found a very simpel solutions:
ofcouse you need to implement OnMenuItemClickListener for this to work