Search code examples
androidgridviewandroid-gridview

How to disable the GridView from being scrolled by a PopupWindow


In my GridView I show a Popupwindow when user makes a long tap. But according to documentation, if there's no room, the Popupwindow tries to scroll the view's parent. And this's what I want to avoid.

showAsDropDown(View anchor) If there is not enough room on screen to show * the popup in its entirety, this method tries to find a parent scroll * view to scroll.

I looked into Popupwindow documentation, and I found the following method to achieve my goal (To avoid scrolling the parent), but it's unsupported for app usage.

/**
 * 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;
}

Solution

  • For future readers, here's how I solved GridView being forced to be scrolled by the Popupwindow.

    I couldn't find an approach to disable GridView's scrolling before showing the Popupwindow. So I made sure that Popupwindow won't appear near the bottom edge.

    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);
        }
    }