PopupMenu使用步驟
//這里的anchor是PopupMenu需要依附的view
PopupMenu popupMenu = new PopupMenu(context, anchor);
//填充PopupMenu內(nèi)容
popupMenu.getMenuInflater().inflate(R.menu.conversation_item_popupmenu, popupMenu.getMenu());
//PopupMenu點(diǎn)擊事件
popupMenu.setOnMenuItemClickListener(onMenuItemClickListener);
//PopupMenu消失事件
popupMenu.setOnDismissListener(onDismissListener);
//彈出PopupMenu荞驴,默認(rèn)gravity為Gravity.START
popupMenu.show();
彈出效果如下圖所示
可以使用setGravity()方法來指定彈出窗口與anchor視圖的對齊方式溺健,例如修改對齊方式為Gravity.END
使用起來還是比較簡單的像棘,但是好像大部分項(xiàng)目的需求是PopupMenu在用戶點(diǎn)擊的位置彈出安券,然而PopupMenu并沒有提供在指定坐標(biāo)彈出的方法,所以只能咱們自己來實(shí)現(xiàn)咯!
想讓PopupMenu在指定彈出位置,首先咱們得先了解show()方法是如何讓PopupMenu彈出來的庶溶,所以只能去閱讀源碼了(Read The Fucking Source Code~)。
public void show() {
mPopup.show();
}
PopupMenu的show()方法很簡單懂鸵,直接把任務(wù)轉(zhuǎn)給MenuPopupHelper來處理偏螺,處理流程:show() -> tryShow() -> showPopup(0, 0, false, false);
public void show() {
if (!tryShow()) {
throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
}
}
//安全判斷,檢測是否有anchor view
public boolean tryShow() {
if (isShowing()) {
return true;
}
if (mAnchorView == null) {
return false;
}
//這就是我們要找的方法匆光,默認(rèn)坐標(biāo)是(0,0)
showPopup(0, 0, false, false);
return true;
}
//xOffset 相對于anchor視圖的x坐標(biāo)
//yOffset 相對于anchor視圖的y坐標(biāo)
private void showPopup(int xOffset, int yOffset, boolean useOffsets, boolean showTitle) {
final MenuPopup popup = getPopup();
popup.setShowTitle(showTitle);
if (useOffsets) {
// If the resolved drop-down gravity is RIGHT, the popup's right
// edge will be aligned with the anchor view. Adjust by the anchor
// width such that the top-right corner is at the X offset.
final int hgrav = GravityCompat.getAbsoluteGravity(mDropDownGravity,
ViewCompat.getLayoutDirection(mAnchorView)) & Gravity.HORIZONTAL_GRAVITY_MASK;
if (hgrav == Gravity.RIGHT) {
xOffset -= mAnchorView.getWidth();
}
popup.setHorizontalOffset(xOffset);
popup.setVerticalOffset(yOffset);
// Set the transition epicenter to be roughly finger (or mouse
// cursor) sized and centered around the offset position. This
// will give the appearance that the window is emerging from
// the touch point.
final float density = mContext.getResources().getDisplayMetrics().density;
final int halfSize = (int) (TOUCH_EPICENTER_SIZE_DP * density / 2);
final Rect epicenter = new Rect(xOffset - halfSize, yOffset - halfSize,
xOffset + halfSize, yOffset + halfSize);
popup.setEpicenterBounds(epicenter);
}
popup.show();
}
我們可以看到showPopup方法內(nèi)有兩個(gè)參數(shù)int xOffset套像、int yOffset,根據(jù)注釋可以知道這就是相對于anchor視圖的坐標(biāo)值终息。所以如果要指定PopupMenu的彈出位置凉夯,MenuPopupHelper應(yīng)該這樣處理彈出邏輯:show(int x, int y) -> tryShow(int x, int y) -> showPopup(x, y, true, true)货葬。
但是由于PopupMenu無法調(diào)用到MenuPopupHelper的show(int x, int y) 方法,因此我們只能使用反射機(jī)制繞過PopupMenu劲够,直接調(diào)用MenuPopupHelper的show(int x, int y)方法。
到此為止休傍,已經(jīng)有了大致的解決思路征绎,接下來看看具體實(shí)現(xiàn)。
/**
* 彈出菜單欄
*
* @param context
* @param anchor 觸發(fā)事件的View
* @param menuRes 菜單欄內(nèi)容
* @param point 點(diǎn)擊事件相對整個(gè)屏幕的坐標(biāo)
* @param onMenuItemClickListener 菜單欄點(diǎn)擊事件
* @param onDismissListener 菜單欄消失
*/
public static void showPopupMenu(Context context, View anchor, @MenuRes int menuRes, Point point,
PopupMenu.OnMenuItemClickListener onMenuItemClickListener,
PopupMenu.OnDismissListener onDismissListener) {
//這里的anchor代表PopupMenu需要依附的view
PopupMenu popupMenu = new PopupMenu(context, anchor);
//填充PopupMenu內(nèi)容
popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu());
//PopupMenu點(diǎn)擊事件
popupMenu.setOnMenuItemClickListener(onMenuItemClickListener);
//PopupMenu消失事件
popupMenu.setOnDismissListener(onDismissListener);
//通過反射機(jī)制修改彈出位置磨取,在點(diǎn)擊的位置彈出PopupMenu
try {
//獲取PopupMenu類的成員變量MenuPopupHelper mPopup
Field mPopup = popupMenu.getClass().getDeclaredField("mPopup");
mPopup.setAccessible(true);
Object o = mPopup.get(popupMenu);
//MenuPopupHelper -> show(int x, int y)方法
if (o instanceof MenuPopupHelper) {
MenuPopupHelper menuPopupHelper = (MenuPopupHelper) o;
int[] position = new int[2];
//獲取anchor左上角在屏幕上的相對坐標(biāo)
anchor.getLocationInWindow(position);
//計(jì)算xOffset人柿、yOffset,相對anchor左下角位置為彈出位置
int xOffset = (point.x - position[0]);
int yOffset;
//菜單高度
int popupMenuHeight = DensityUtil.dp2px(context, 48 * popupMenu.getMenu().size());
//如果菜單高度大于底部剩余空間忙厌,菜單就會向上彈出凫岖;否則向下彈出
if (ScreenUtil.getScreenHeight(context) - point.y >= popupMenuHeight) {
yOffset = (point.y - (position[1] + anchor.getHeight()));
} else {
yOffset = (point.y - (position[1]));
}
menuPopupHelper.show(xOffset, yOffset);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
//出錯(cuò)時(shí)調(diào)用普通show方法。未出錯(cuò)時(shí)此方法也不會影響正常顯示
popupMenu.show();
}
}
最終彈出效果如下圖所示