我无法在调用的ImageView下面显示一个弹出窗口

时间:2015-03-30 14:35:11

标签: android popupwindow

我有一个ListView的项目,每个项目都有一个ImageView,通过点击它显示一个PopupWindow。问题是我无法显示调用的ImageView下面的窗口。调用pw的ImageView的OnClickListener是在listadapter类的getView()方法中实现的。

如何在相应的ImageView下方显示pw?

的ListView

enter image description here

我尝试使用此代码执行此操作,但它无效。永远不会调用onGlobalLayout()。

@Override public View getView(int position, View convertView, final ViewGroup parent) {

        ...

        // ImageView that show the PopupWindow
        holder.menuOverflow = (ImageView) row.findViewById(R.id.iv_menu_overflow_item_listview_seccion);
        holder.menuOverflow.setTag(position);
        holder.menuOverflow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {
                ViewTreeObserver vto = (holder.menuOverflow).getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        y = (int) (holder.menuOverflow).getY();
                    }
                });

                ...

                // Show the PopupWindow
                pw.showAtLocation(parent, Gravity.RIGHT, 0, y);

                // Animation
                Animation anim = AnimationUtils.loadAnimation(activity, R.anim.grow_from_top);
                anim.setDuration(100);
                popupView.startAnimation(anim);
            }
        });

        return row;
    }

1 个答案:

答案 0 :(得分:0)

由我解决:

int[] _location = new int[2];

// Point (x, y) stored in array _location
// _location[0] = x, _location[1] = y.
v.getLocationOnScreen(_location);

//Initialize the Point with x, and y positions
Point p = new Point();
p.x = _location[0];
p.y = _location[1];

int OFFSET_Y = 30;

// muestra la popup window
pw.showAtLocation(activity.findViewById(R.id.sliding_panel_layout), Gravity.NO_GRAVITY, p.x, p.y + OFFSET_Y);
相关问题