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

时间:2017-10-14 13:42:51

标签: android popup android-popupwindow

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

当触摸的视图底部为on时,弹出菜单会隐藏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);

    }

2 个答案:

答案 0 :(得分:0)

这就是我所做的:

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) {

结果如下:

enter image description here

enter image description here

答案 1 :(得分:0)

发现了。 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:https://stackoverflow.com/a/15862282/2700303
        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);

    }