2
我正在構建一個自定義組件,我希望在上面有上下文菜單。到目前爲止,我已經成功地創建並顯示上下文菜單執行以下操作:自定義組件中的上下文菜單
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成員。我不確定這是否可以這樣做。有什麼建議麼?