MenuPopupHelperpublic class MenuPopupHelper extends Object implements PopupWindow.OnDismissListener, MenuPresenter, View.OnKeyListener, ViewTreeObserver.OnGlobalLayoutListener, AdapterView.OnItemClickListenerPresents a menu as a small, simple popup anchored to another view. |
Fields Summary |
---|
private static final String | TAG | static final int | ITEM_LAYOUT | private final android.content.Context | mContext | private final android.view.LayoutInflater | mInflater | private final MenuBuilder | mMenu | private final MenuAdapter | mAdapter | private final boolean | mOverflowOnly | private final int | mPopupMaxWidth | private final int | mPopupStyleAttr | private final int | mPopupStyleRes | private android.view.View | mAnchorView | private android.support.v7.widget.ListPopupWindow | mPopup | private android.view.ViewTreeObserver | mTreeObserver | private Callback | mPresenterCallback | boolean | mForceShowIcon | private android.view.ViewGroup | mMeasureParent | private boolean | mHasContentWidthWhether the cached content width value is valid. | private int | mContentWidthCached content width from {@link #measureContentWidth}. | private int | mDropDownGravity |
Constructors Summary |
---|
public MenuPopupHelper(android.content.Context context, MenuBuilder menu)
this(context, menu, null, false, R.attr.popupMenuStyle);
| public MenuPopupHelper(android.content.Context context, MenuBuilder menu, android.view.View anchorView)
this(context, menu, anchorView, false, R.attr.popupMenuStyle);
| public MenuPopupHelper(android.content.Context context, MenuBuilder menu, android.view.View anchorView, boolean overflowOnly, int popupStyleAttr)
this(context, menu, anchorView, overflowOnly, popupStyleAttr, 0);
| public MenuPopupHelper(android.content.Context context, MenuBuilder menu, android.view.View anchorView, boolean overflowOnly, int popupStyleAttr, int popupStyleRes)
mContext = context;
mInflater = LayoutInflater.from(context);
mMenu = menu;
mAdapter = new MenuAdapter(mMenu);
mOverflowOnly = overflowOnly;
mPopupStyleAttr = popupStyleAttr;
mPopupStyleRes = popupStyleRes;
final Resources res = context.getResources();
mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
res.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
mAnchorView = anchorView;
// Present the menu using our context, not the menu builder's context.
menu.addMenuPresenter(this, context);
|
Methods Summary |
---|
public boolean | collapseItemActionView(MenuBuilder menu, MenuItemImpl item)
return false;
| public void | dismiss()
if (isShowing()) {
mPopup.dismiss();
}
| public boolean | expandItemActionView(MenuBuilder menu, MenuItemImpl item)
return false;
| public boolean | flagActionItems()
return false;
| public int | getId()
return 0;
| public MenuView | getMenuView(android.view.ViewGroup root)
throw new UnsupportedOperationException("MenuPopupHelpers manage their own views");
| public android.support.v7.widget.ListPopupWindow | getPopup()
return mPopup;
| public void | initForMenu(android.content.Context context, MenuBuilder menu)
// Don't need to do anything; we added as a presenter in the constructor.
| public boolean | isShowing()
return mPopup != null && mPopup.isShowing();
| private int | measureContentWidth()
// Menus don't tend to be long, so this is more sane than it looks.
int maxWidth = 0;
View itemView = null;
int itemType = 0;
final ListAdapter adapter = mAdapter;
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int count = adapter.getCount();
for (int i = 0; i < count; i++) {
final int positionType = adapter.getItemViewType(i);
if (positionType != itemType) {
itemType = positionType;
itemView = null;
}
if (mMeasureParent == null) {
mMeasureParent = new FrameLayout(mContext);
}
itemView = adapter.getView(i, itemView, mMeasureParent);
itemView.measure(widthMeasureSpec, heightMeasureSpec);
final int itemWidth = itemView.getMeasuredWidth();
if (itemWidth >= mPopupMaxWidth) {
return mPopupMaxWidth;
} else if (itemWidth > maxWidth) {
maxWidth = itemWidth;
}
}
return maxWidth;
| public void | onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)
// Only care about the (sub)menu we're presenting.
if (menu != mMenu) return;
dismiss();
if (mPresenterCallback != null) {
mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
}
| public void | onDismiss()
mPopup = null;
mMenu.close();
if (mTreeObserver != null) {
if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver();
mTreeObserver.removeGlobalOnLayoutListener(this);
mTreeObserver = null;
}
| public void | onGlobalLayout()
if (isShowing()) {
final View anchor = mAnchorView;
if (anchor == null || !anchor.isShown()) {
dismiss();
} else if (isShowing()) {
// Recompute window size and position
mPopup.show();
}
}
| public void | onItemClick(android.widget.AdapterView parent, android.view.View view, int position, long id)
MenuAdapter adapter = mAdapter;
adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
| public boolean | onKey(android.view.View v, int keyCode, android.view.KeyEvent event)
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
dismiss();
return true;
}
return false;
| public void | onRestoreInstanceState(android.os.Parcelable state)
| public android.os.Parcelable | onSaveInstanceState()
return null;
| public boolean | onSubMenuSelected(SubMenuBuilder subMenu)
if (subMenu.hasVisibleItems()) {
MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView);
subPopup.setCallback(mPresenterCallback);
boolean preserveIconSpacing = false;
final int count = subMenu.size();
for (int i = 0; i < count; i++) {
MenuItem childItem = subMenu.getItem(i);
if (childItem.isVisible() && childItem.getIcon() != null) {
preserveIconSpacing = true;
break;
}
}
subPopup.setForceShowIcon(preserveIconSpacing);
if (subPopup.tryShow()) {
if (mPresenterCallback != null) {
mPresenterCallback.onOpenSubMenu(subMenu);
}
return true;
}
}
return false;
| public void | setAnchorView(android.view.View anchor)
mAnchorView = anchor;
| public void | setCallback(Callback cb)
mPresenterCallback = cb;
| public void | setForceShowIcon(boolean forceShow)
mForceShowIcon = forceShow;
| public void | setGravity(int gravity)
mDropDownGravity = gravity;
| public void | show()
if (!tryShow()) {
throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
}
| public boolean | tryShow()
mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
mPopup.setOnDismissListener(this);
mPopup.setOnItemClickListener(this);
mPopup.setAdapter(mAdapter);
mPopup.setModal(true);
View anchor = mAnchorView;
if (anchor != null) {
final boolean addGlobalListener = mTreeObserver == null;
mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest
if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this);
mPopup.setAnchorView(anchor);
mPopup.setDropDownGravity(mDropDownGravity);
} else {
return false;
}
if (!mHasContentWidth) {
mContentWidth = measureContentWidth();
mHasContentWidth = true;
}
mPopup.setContentWidth(mContentWidth);
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopup.show();
mPopup.getListView().setOnKeyListener(this);
return true;
| public void | updateMenuView(boolean cleared)
mHasContentWidth = false;
if (mAdapter != null) {
mAdapter.notifyDataSetChanged();
}
|
|