【问题标题】:How to adjust popup menu when pop outside container弹出容器时如何调整弹出菜单
【发布时间】:2017-10-14 23:01:03
【问题描述】:

我正在使用在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);

    }

【问题讨论】:

    标签: android popup android-popupwindow


    【解决方案1】:

    这就是我所做的:

    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】:

    发现了。 2 事情正在进行中,并在 cmets 中进行了解释。

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

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多