PopupWindow--点击ListView的Item,popupwindow出现在点击item下方

1.原理

ListView的 public void onItemClick(AdapterView adapterView, View view, int position, long l)的view是所点击的Item的对应View,通过popupwindow.showAsDropDown(anchor);通过anchor的位置显示popupwindow

代码如下

/**
     * show the reason why don't supply the dish by floatview
     *
     * @param anchor
     * @param item
     * @param curViewY
     */
    private void showReason(View anchor, ConsignorDataRangeTimeItem item, int curViewY) {
        PopupView popup = new PopupView(this);
        View view = View.inflate(this, R.layout.consignor_reason_notsupply, null);
        
        popup.initView(this, view, popup.screenWidth / 2, WindowManager.LayoutParams.WRAP_CONTENT);
        if(popup.needReverse(anchor,curViewY)){
            view.setBackgroundResource(R.drawable.popup_view_bg_reverse);
        }
        popup.showView(anchor, 0, 0);

    }

    @Override
    public void onItemClick(AdapterView adapterView, View view, int position, long l) {
        int id = adapterView.getId();
        switch (id) {
            case R.id.consignor_data_range_list:
                ConsignorDataRangeTimeItem item = list.get(position);
                if (item.getNotCount() == 0)
                    return;
                View anchor = view.findViewById(R.id.consignor_data_time_range__item_dropdown);
                int[]scr=new int[2];
                view.getLocationOnScreen(scr);//获取view的绝对位置
                showReason(anchor, item, scr[1]);
        }
    }


    public static class PopupView {
        private final Context mContext;
        private PopupWindow mPopup;
        private WindowManager wm;
        public int screenWidth;
        public int screenHeight;

        public PopupView(Context context) {
            mContext = context;
        }

        /**
         * reset the popupwindow
         */
        public void reset() {
            mPopup.dismiss();
            mPopup = null;
        }

        public void setScreen() {
            wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            //设置LayoutParams(全局变量)相关参数
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();

            //获取屏幕尺寸
            //Display display=wm.getDefaultDisplay();
            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels;
        }

        /**
         * init the popupview
         *
         * @param context
         * @param view    the view add to popup view
         */
        protected void initView(Context context, View view, int width, int height) {
            // TODO Auto-generated method stub
            if (width == 0 || height == 0) {
                setScreen();
                width = screenWidth / 2;
                height = WindowManager.LayoutParams.WRAP_CONTENT;
            }
            mPopup = new PopupWindow(view, width, height);
        /*设置背景显示 */
            //menuView.setBackgroundResource(R.color.transparent);
            //设置触摸外面时消失
            mPopup.setOutsideTouchable(true);
            //设置系统动画
            mPopup.setAnimationStyle(android.R.style.Animation_Dialog);
            mPopup.setTouchable(true);
            mPopup.setBackgroundDrawable(new BitmapDrawable());
            mPopup.update();
            //设置了可获得焦点才能让Popu盘Window中的控件获取相应
            mPopup.setFocusable(true);

        }

        /**
         * show popup view
         *
         * @param anchor  a parent view to get the getWindowToken() token from
         * @param offsetX
         * @param offsetY
         */
        public void showView(View anchor, int offsetX, int offsetY) {
            // TODO Auto-generated method stub
            mPopup.showAsDropDown(anchor);
            //mPopup.showAtLocation(anchor, Gravity.RIGHT|Gravity.TOP, offsetX, offsetY);
        }

        /**
         * hide the popup view
         */
        public void hideView() {
            if (mPopup.isShowing())
                mPopup.dismiss();

        }

        /**
         *
         * @param anchor
         * @param curViewY
         * @return if curViewY>=maxHeight return true;
         */
        public boolean needReverse(View anchor, int curViewY) {
            int maxHeight=   mPopup.getMaxAvailableHeight(anchor);
            return curViewY>=maxHeight;
        }
    }


 
 

你可能感兴趣的:(Android开发)