PopupMenu click causing RecyclerView to scroll

无人久伴 提交于 2019-12-04 01:52:52

Tried using android.widget.PopupMenu instead of android.support.v7.widget.PopupMenu and Voila, it works. So is it a bug in Support library. Can great developers out here confirm the same?

You can have your AnchorView override requestRectangleOnScreen() and return false. This will prevent any parent ScrollView from scrolling.

So, in your case, imgBtnOverflow would be a custom ImageButton, like so:

/**
 * A custom {@link ImageButton} which prevents parent ScrollView scrolling when used as the
 * anchor for a {@link android.support.v7.widget.PopupMenu}
 */
public class NonScrollImageButton extends ImageButton {

    public NonScrollImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
        return false;
    }
}

Props to jankovd (https://gist.github.com/jankovd/19ef35efd1f00e9213fa)

An issue has been filed against the Support PopupMenu here: https://code.google.com/p/android/issues/detail?id=135439

I have same problem, and I use this way:

mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) {
            @Override
            public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
                return false;
            }
        };
mRecyclerView.setLayoutManager(mLayoutManager);

RecyclerView user layoutmanager to manage layout, so I think change layoutmanger behaver is the bettor solution.

requestRectangleOnScreennot work for me, in my case, requestRectangleOnScreennot called, when show popupMenu. my solution is override requestChildRectangleOnScreen, and reture false, work well.

public class PopupAncorRecyclerViewPager extends RecyclerView {
        @Override
    public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
        return false;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!