Android 如何检测ListPopupWindow是否显示在锚定视图的上方或下方?
我正在显示带有锚定视图的ListPopupWindow。我需要显示ListPopupWindow相对于锚定视图的不同位置的不同内容 例如,如果ListPopupWindow显示在锚定视图上方,则其内容将不同;如果ListPopupWindow显示在锚定视图下方,则其内容将不同 我的问题是如何检测ListPopupWindow的垂直位置?它是否位于锚定视图的上方或下方。Android 如何检测ListPopupWindow是否显示在锚定视图的上方或下方?,android,android-layout,Android,Android Layout,我正在显示带有锚定视图的ListPopupWindow。我需要显示ListPopupWindow相对于锚定视图的不同位置的不同内容 例如,如果ListPopupWindow显示在锚定视图上方,则其内容将不同;如果ListPopupWindow显示在锚定视图下方,则其内容将不同 我的问题是如何检测ListPopupWindow的垂直位置?它是否位于锚定视图的上方或下方。 提前感谢 ListPopupWindow有一个PopupWindow作为默认成员,因此在包外无法访问它。所以你应该使用反射 创建
提前感谢 ListPopupWindow有一个PopupWindow作为默认成员,因此在包外无法访问它。所以你应该使用反射 创建一个扩展类并在ctor中使用此代码
try {
Field f = ListPopupWindow.class.getDeclaredField("mPopup");
if (f != null) {
f.setAccessible(true);
Object obj = f.get(this);
if (obj != null) {
mSuperPopup = (PopupWindow) obj;
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
private PopupWindow mSuperPopup = null;
public boolean isAboveAnchor() {
if (mSuperPopup != null)
return mSuperPopup.isAboveAnchor();
return false;
}
只需声明ListPopupWindow listPopup;作为全局,并检查(listPopup.isShowing())listPopup.else是否创建新实例