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(fi开发者_Python百科nal 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?
Well after searching for other options i found a very simpel solutions:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menuInflator.inflate(R.menu.menu_context, menu);
menu.getItem(0).setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// Do something
return false;
}
ofcouse you need to implement OnMenuItemClickListener for this to work
精彩评论