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

代码如下

<pre name="code" class="java">/**
     * 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;
        }
    }



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值