PopupWindow-----listview item的点击出现PopupWindow
/** * 设置listview item的点击事件 */ lv_app_manager.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (position == 0) { return; } else if (position == (userAppInfos.size() + 1)) { //超过不弹出 return; } else if (position <= userAppInfos.size()) {// 用户程序 int newposition = position - 1; appInfo = userAppInfos.get(newposition); //appInfo单个对象 } else {// 系统程序 int newposition = position - 1 - userAppInfos.size() - 1; appInfo = systemAppInfos.get(newposition); // 点击 } // System.out.println(appInfo.getPackname()); dismissPopupWindow(); View contentView = View.inflate(getApplicationContext(), R.layout.popup_app_item, null); ll_start = (LinearLayout) contentView .findViewById(R.id.ll_start); ll_share = (LinearLayout) contentView .findViewById(R.id.ll_share); ll_uninstall = (LinearLayout) contentView .findViewById(R.id.ll_uninstall); //卸载 ll_start.setOnClickListener(AppManagerActivity.this); ll_share.setOnClickListener(AppManagerActivity.this); ll_uninstall.setOnClickListener(AppManagerActivity.this); // 得到 PopupWindow 的对象 popupWindow = new PopupWindow(contentView, -2, -2); // 上面的contentView // 动画效果的播放必须要求窗体有背景颜色。 // 透明颜色也是颜色 popupWindow.setBackgroundDrawable(new ColorDrawable( Color.TRANSPARENT)); int[] location = new int[2]; view.getLocationInWindow(location); // 在代码里面设置的宽高值 都是像素。---》dip int dip = 60; int px = DensityUtil.dip2px(getApplicationContext(), dip); System.out.println("px=" + px); // 得到 px popupWindow.showAtLocation(parent, Gravity.LEFT | Gravity.TOP, px, location[1]); // 最后加上动画 ScaleAnimation sa = new ScaleAnimation(0.3f, 1.0f, 0.3f, 1.0f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(300); AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f); aa.setDuration(300); AnimationSet set = new AnimationSet(false); set.addAnimation(aa); set.addAnimation(sa); contentView.startAnimation(set); } }); 1.关闭的方法 private void dismissPopupWindow() { // 把旧的弹出窗体关闭掉。 if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } }