Android----PopupWindow

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:
  AlertDialog的位置固定,而PopupWindow的位置可以随意
  AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

 

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;

按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。

具体如下:
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

 

具体的Demo:

  1 public class SearchPop{
  2     private PopupWindow pop;
  3     private Context context;
  4     // 列表弹窗的间隔
  5     protected final int LIST_PADDING = 10;
  6     // 实例化一个矩形
  7     private Rect mRect = new Rect();
  8     // 坐标的位置(x,y)
  9     private final int[] mLocation = new int[2];
 10     // 位置不在中心
 11     private int popupGravity = Gravity.NO_GRAVITY;
 12     // 弹窗子类项选中时的回调监听接口
 13     private OnItemClickListener mItemListener;
 14     // 定义列表对象
 15     private ListView mListView;
 16     // 定义列表子类项 数据集合
 17     private ArrayList<String> mData = new ArrayList<String>();
 18 
 19     private void setData(ArrayList<String> mData){
 20         if(mData != null && !mData.isEmpty())
 21             this.mData = mData;
 22         else
 23             this.mData = new ArrayList<String>();
 24     }
 25     
 26     public SearchPop(Context context, ArrayList<String> mData) {
 27         this.setData(mData);
 28         this.context = context;
 29         View view = LayoutInflater.from(context).inflate(R.layout.popup_window, null);
 30         pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 31         pop.setContentView(view);
 32         // 设置可以获取焦点
 33         pop.setFocusable(true);
 34         // 设置弹窗内可点击
 35         pop.setTouchable(true);
 36         // 设置弹窗外可点击
 37         pop.setOutsideTouchable(true);
 38         // 设置popupWindows的宽度和高度
 39         pop.setWidth(LayoutParams.WRAP_CONTENT);
 40         pop.setHeight(LayoutParams.WRAP_CONTENT);
 41         // 设置背景
 42         // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的)
 43         pop.setBackgroundDrawable(new BitmapDrawable());
 44         // 设置popupWindow的布局界面
 45 
 46         setupView();
 47     }
 48 
 49     private void setupView() {
 50         mListView = (ListView) pop.getContentView().findViewById(R.id.lvPopupWindow);
 51         mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 52             @Override
 53             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 54                 //点击item,popupWindow消失
 55                 pop.dismiss();
 56                 if(mItemListener != null)
 57                     mItemListener.onItemClick(mData.get(position), position);
 58             }
 59         });
 60     }
 61 
 62     public void showPopupWindow(View view) {
 63         // 获得点击屏幕的位置坐标
 64         view.getLocationOnScreen(mLocation);
 65         // 设置矩形大小
 66         mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),
 67                 mLocation[1] + view.getHeight());
 68         // 显示popupWindow的位置
 69         pop.showAtLocation(view, popupGravity, LIST_PADDING, mLocation[1]+LIST_PADDING*2);
 70         mListView.setAdapter(myAdapter);
 71     }
 72     
 73     private BaseAdapter myAdapter = new BaseAdapter() {
 74         @Override
 75         public View getView(int position, View convertView, ViewGroup parent) {
 76             TextView tvText = null;
 77             convertView = LayoutInflater.from(context).inflate(R.layout.popup_window_item, null);
 78             tvText = (TextView) convertView.findViewById(R.id.tvPopupWindowItem);
 79             String item = getItem(position);
 80             tvText.setText(item);
 81             return convertView;
 82         }
 83         @Override
 84         public long getItemId(int position) {
 85             return position;
 86         }
 87         @Override
 88         public String getItem(int position) {
 89             return mData.get(position);
 90         }
 91         @Override
 92         public int getCount() {
 93             return mData.size();
 94         }
 95     };
 96     
 97     /**
 98      * 根据位置获得item
 99      * @param position
100      * @return
101      */
102     public String getAction(int position){
103         if(position < 0 || position>mData.size()){
104             return null;
105         }
106         return mData.get(position);
107     }
108     
109     /**
110      * 设置监听事件
111      * @param mItemListener
112      */
113     public void setItemClickListener(OnItemClickListener mItemListener){
114         this.mItemListener = mItemListener;
115     }
116     
117     /**
118      * 回调接口
119      * @author Administrator
120      *
121      */
122     public static interface OnItemClickListener{
123         void onItemClick(String item , int position);
124     }
125     
126 }

 

 

然后,在对button的事件监听中,调用showPopupWindow()方法,来实例化这个popupwindow.

在回调里面,写这个popupWindow的子类项点击事件的处理方法

pop.setItemClickListener(new OnItemClickListener){

  public void onItemClick(String item, int position){

    //点击 如何处理

  }

}

 

 

 

注意事项:

1、构造popupwindow时,

  pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

一定要给出这个popupWindow的宽和高。 官方文档说明的很清楚,当不传递width,height参数时,构造出来的popupWindow的dimension只有0*0

 

2、在popupWindow中显示listview

1 pop.setFocusable(true)  

如果不设置, 则点击不到listview

 

3、点击popupWindow退出,另外还想点击屏幕其他位置退出,点击“back”键退出的话:

//设置外部可以点击
pop.setOutsideTouchAble(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的)
pop.setBackgroundDrawable(new BitmapDrawable());

 

 

 

转载于:https://www.cnblogs.com/android-madan/p/3458976.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值