如何禁用GridView不受PopupWindow滚动

时间:2020-02-02 18:45:46

标签: android gridview android-gridview

在我的GridView中,当用户长按时会显示一个Popupwindow。但是根据文档,如果没有空间,则Popupwindow会尝试滚动视图的父级。 这就是我要避免的事情。

showAsDropDown(查看锚点)如果屏幕上没有足够的空间显示 *弹出窗口的全部内容,此方法尝试查找父级滚动 *查看即可滚动。

我查看了Popupwindow文档,发现以下方法可以实现我的目标(避免滚动父对象),但是不支持应用使用。

/**
 * Allow PopupWindow to scroll the anchor's parent to provide more room
 * for the popup. Enabled by default.
 *
 * @param enabled True to scroll the anchor's parent when more room is desired by the popup.
 */

@UnsupportedAppUsage
void setAllowScrollingAnchorParent(boolean enabled) {
    mAllowScrollingAnchorParent = enabled;
}

1 个答案:

答案 0 :(得分:0)

对于将来的读者,这是我解决了GridView被Popupwindow强制滚动的方法。

在显示Popupwindow之前,我找不到禁用GridView滚动的方法。因此,我确保Popupwindow不会出现在底部边缘附近。

public void showDropDownMenu(View aView, PopupWindow aPopupWindow, int aMnuItemsNum){
    int[] loc = new int[2];
    aView.getLocationOnScreen(loc);

    int popHeight = (toPixels(getMnuItemHeightDip()) * aMnuItemsNum) + aView.getHeight();

    if(getResources().getDisplayMetrics().heightPixels - loc[1] > popHeight){
        aPopupWindow.showAsDropDown(aView);
    } else {
        aPopupWindow.showAsDropDown(aView, 0, - popHeight, Gravity.START | Gravity.TOP);
    }
}
相关问题