• 这里在ListView的条目中弹出popupWindow
  • 跟着步骤一个一个来
private PopupWindow mPw;
    private ListView mListView;
  • 这是在点击事件中处理的操作
//这里是在ListView的点击事件中弹出气泡 `listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
  //第二个参数view是单个条目的view,后面会使用到!
            @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
//第五步,在显示气泡前先判断一下旧的气泡是否存在,存在了就隐藏
 if (mPw != null){
            mPw.dismiss();
    }
    //=null 为了显示下一次新的气泡做准备
    mPw = null; 

//第一步,创建一个需要传入的布局,就是弹出popup的View
                View popupView = View.inflate(getApplicationContext(), R.layout.popup_window, null);

//第二步,new 出有3个构造函数的对象,参数1:传入的布局 
//参数23:布局显示的宽高
mPw = new PopupWindow(popupView , LinearLayout.LayoutParams.WRAP_CONTENT,                       LinearLayout.LayoutParams.WRAP_CONTENT);

//第六步!!!很重要!!!
//因为popupwindow是没有背景的,所以这里需要设置一个背景,这里设置成透明颜色
                mPw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

//第三步,获取mListView中单个条目在屏幕显示的位置,
//里面参数需要传入一个int[2]的数组,分别代表XY的坐标,即条目左上角的坐标
int[] location = new int[2];
view.getLocationInWindow(location);
//得到坐标
int x = location[0];
int y = location[1];

//第四步,显示气泡
//参数1:需要挂载在哪一个控件上,parent = mListView
//参数2:位置布局  参数34:代表  !!屏幕!!  的X轴和Y轴距离
mPw.showAtLocation(parent , Gravity.LEFT | Gravity.TOP , x+50 , y);   
//第五步显示气泡有动画效果,这里使用属性动画
                //渐变,从半透明变成不透明
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(popupView, "alpha" , 0.4f, 1.0f );
                //缩放,自身的中心 从小变大
                ObjectAnimator animator2 = ObjectAnimator.ofFloat(popupView, "scaleX" , 0.1f, 1.0f );
                ObjectAnimator animator3 = ObjectAnimator.ofFloat(popupView, "scaleY" , 0.1f, 1.0f );
                //属性动画,使用AnimatorSet来执行
                AnimatorSet set = new AnimatorSet();
                //设置动画一起执行, 还是顺序执行 还是有选择的执行,这里选择一起执行
                set.playTogether(animator1 , animator2,animator3);
                //设置动画执行时间和 开始动画的操作
                set.setDuration(500).start();  
 //第六步很重要!!!在上面初始化popupwindow的时候需要设置!!
  • 另一个方法
  • mPW = new PopupWindow(parent , x, y);
  • xy可以设置为需要插入View的宽高
  • mPw.setFocusable(true);意思是设置可以获取焦点
  • mPW.setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));//设置背景色透明
  • mPW.setOutsideTouchable(true);//点击外部区域,自动隐藏
  • mPw.showAsDropDown(view , x ,y )
  • 意思是在第一个参数及控件的下面显示,x y 表示偏移量

  • 这里是在滑动事件中处理的操作
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }
            //滑动的时候调用
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

          //滑动的时候也需要消失气泡
         if (mPw != null){
            mPw.dismiss();
            }
            //=null 为了显示下一次新的气泡做准备
            mPw = null; 
            }
        });
    }
  • 最后一步一定要注意
  • 需要在活动销毁的时候也需要隐藏气泡,虽然不崩溃,但是log会显示错误,也是需要隐藏操作的
@Override
    protected void onDestroy() {
        super.onDestroy();
         //如果activitydestroy的时候也需要消失气泡
         if (mPw != null){
            mPw.dismiss();
            }
            //=null 为了显示下一次新的气泡做准备
            mPw = null; 
            }
    }