弹出容器外时如何调整弹出菜单

How to adjust popup menu when pop outside container

我正在使用在 RecycleView 容器内弹出的自定义弹出菜单。

当触摸的视图在底部打开时,弹出菜单会隐藏RecycleView下方的控件。

在这种情况下,我想调整弹出窗口的位置,将弹出窗口调整到所需的确切距离以便菜单保持在容器内

这应该很容易,但我在获取坐标和应用处理项目点击的 Adapter 内部的计算时遇到困难。

以下是我目前所做的:

void show(FragmentActivity activity, View touchedView, DataItem item) {

        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);

        PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setOutsideTouchable(true);

        bind(popupWindow, popupView, item);

        //draws the menu perfectly bellow the touched element,but doesn't take in account the parent view area
        popupWindow.showAsDropDown(touchView);

    }

这是我所做的:

private void showPopupMenu(final View view, final int position) {
    // inflate menu
    final PopupMenu popup = new PopupMenu(view.getContext(), view);
    popup.setGravity(Gravity.END);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.popup_menu, popup.getMenu());
    popup.show();
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

    final EditText editText = (EditText) promptView.findViewById(R.id.input_text);
                    // setup a dialog window
        alertDialogBuilder.setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {

结果如下:

发现了。 2 事情正在路上,并在评论中解释。

void show(FragmentActivity activity, View touchView, IDataItem dataItem) {

        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);

        PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setOutsideTouchable(true);

        bind(popupWindow, popupView, dataItem);

        //1º problem: View item is inside ViewHolder.Item, so container is 2 levels up and touched item one level up.
        View container = (View) touchView.getParent().getParent();
        View item = (View) touchView.getParent();

        int containerHeight = container.getHeight();

        //2º problem: to get size before the popup view is displayed:  ref:
        popupView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        int yOff=0;

        //if popup view will draw pass the bottom, get the amount needed to adjust the y coordinate
        if (ContainerHeight < item.getHeight() + item.getTop() + popupView.getMeasuredHeight())
            yOff = ContainerHeight- (item.getTop() + item.getHeight() + popupView.getMeasuredHeight());

        popupWindow.showAsDropDown(touchView,0,yOff);

    }