PopupWindow自定義類

public class PopupWindow extends PopupWindow{

    private View conentView;
    private ListView popLv;


    private Window window;
    private WindowManager.LayoutParams layoutParams;
    private Activity context;
    private int type=0;

    public   PopupWindow(final Activity context) {
        this.context=context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.activity_popupwindow_list, null);
        //屏幕的寬和高
        int height = context.getWindowManager().getDefaultDisplay().getHeight();
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        // 設置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 設置SelectPicPopupWindow彈出窗體的寬
        this.setWidth(width / 2 -80);
        // 設置SelectPicPopupWindow彈出窗體的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        // 設置SelectPicPopupWindow彈出窗體可點擊
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新狀態
        this.update();
        // 實例化一個ColorDrawable顏色爲半透明
        ColorDrawable dw = new ColorDrawable(/*context.getResources().getColor(R.color.white)*/0000000000);
        // 點back鍵和其他地方使其消失,設置了這個才能觸發OnDismisslistener ,設置其他控件變化等操作
        this.setBackgroundDrawable(dw);
        //設置窗口的透明度
        lp =context.getWindow().getAttributes();
        lp.alpha=0.7f;//0.0爲黑色,1.0透明
        context.getWindow().setAttributes(lp);
        //設置pop隱藏時背景變成透明
        this.setOnDismissListener(new poponDismissListener());

        popLv= (ListView) conentView.findViewById(R.id. lv);

        imageList.add(R.mipmap.ic);
        textViewList.add("111");
        imageList.add(R.mipmap.ic);
        textViewList.add("2222");
        popLv.setAdapter(new PopupWindowAdapter(context,imageList,textViewList));

        popLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position==0){

                    PopupWindow.this.dismiss();

                }else if (position==1){
                    context.startActivity(new Intent(UIUtils.getContext(), XXX.class));
                    PopupWindow.this.dismiss();
                    lp.alpha=1.0f;
                    context.getWindow().setAttributes(lp);
                }
            }
        });

    }
    WindowManager.LayoutParams lp;
    //調用此方法即可parent是顯示在那個控件下
    public void showPopupWindow(View parent) {


        if (!this.isShowing()) {
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 10);

        } else {
            this.dismiss();
            context.getWindow().setAttributes(lp);
        }
    }
    class PopupWindowAdapter extends BaseAdapter {

        private Context context;
        private List<Integer> imageList;
        private List<String> textViewList;
          PopupWindowAdapter(Context context,List<Integer> imageList,List<String> textViewList){
            this.context=context;

            this.imageList=imageList;
            this.textViewList=textViewList;
        }

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHoldr viewHoldr;
            if (convertView==null){
                viewHoldr=new ViewHoldr();
                convertView= LayoutInflater.from(context).inflate(R.layout.item_  popupwindow,null);
                viewHoldr.popImage= (ImageView) convertView.findViewById(R.id.pop  _image);
                viewHoldr.popTv= (TextView) convertView.findViewById(R.id.pop_tv);
                convertView.setTag(viewHoldr);
            }else{
                viewHoldr = (ViewHoldr)convertView.getTag();
            }
            viewHoldr.popImage.setImageResource(imageList.get(position));
            viewHoldr.popTv.setText(textViewList.get(position));

            return convertView;
        }
        class ViewHoldr{
            private ImageView popImage;
            private TextView popTv;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章