您的位置:首页 > 移动开发 > Android开发

Android----PopupWindow

2013-12-05 10:26 85 查看
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:

public class SearchPop{
private PopupWindow pop;
private Context context;
// 列表弹窗的间隔
protected final int LIST_PADDING = 10;
// 实例化一个矩形
private Rect mRect = new Rect();
// 坐标的位置(x,y)
private final int[] mLocation = new int[2];
// 位置不在中心
private int popupGravity = Gravity.NO_GRAVITY;
// 弹窗子类项选中时的回调监听接口
private OnItemClickListener mItemListener;
// 定义列表对象
private ListView mListView;
// 定义列表子类项 数据集合
private ArrayList<String> mData = new ArrayList<String>();

private void setData(ArrayList<String> mData){
if(mData != null && !mData.isEmpty())
this.mData = mData;
else
this.mData = new ArrayList<String>();
}

public SearchPop(Context context, ArrayList<String> mData) {
this.setData(mData);
this.context = context;
View view = LayoutInflater.from(context).inflate(R.layout.popup_window, null);
pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pop.setContentView(view);
// 设置可以获取焦点
pop.setFocusable(true);
// 设置弹窗内可点击
pop.setTouchable(true);
// 设置弹窗外可点击
pop.setOutsideTouchable(true);
// 设置popupWindows的宽度和高度
pop.setWidth(LayoutParams.WRAP_CONTENT);
pop.setHeight(LayoutParams.WRAP_CONTENT);
// 设置背景
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的)
pop.setBackgroundDrawable(new BitmapDrawable());
// 设置popupWindow的布局界面

setupView();
}

private void setupView() {
mListView = (ListView) pop.getContentView().findViewById(R.id.lvPopupWindow);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击item,popupWindow消失
pop.dismiss();
if(mItemListener != null)
mItemListener.onItemClick(mData.get(position), position);
}
});
}

public void showPopupWindow(View view) {
// 获得点击屏幕的位置坐标
view.getLocationOnScreen(mLocation);
// 设置矩形大小
mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),
mLocation[1] + view.getHeight());
// 显示popupWindow的位置
pop.showAtLocation(view, popupGravity, LIST_PADDING, mLocation[1]+LIST_PADDING*2);
mListView.setAdapter(myAdapter);
}

private BaseAdapter myAdapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tvText = null;
convertView = LayoutInflater.from(context).inflate(R.layout.popup_window_item, null);
tvText = (TextView) convertView.findViewById(R.id.tvPopupWindowItem);
String item = getItem(position);
tvText.setText(item);
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public int getCount() {
return mData.size();
}
};

/**
* 根据位置获得item
* @param position
* @return
*/
public String getAction(int position){
if(position < 0 || position>mData.size()){
return null;
}
return mData.get(position);
}

/**
* 设置监听事件
* @param mItemListener
*/
public void setItemClickListener(OnItemClickListener mItemListener){
this.mItemListener = mItemListener;
}

/**
* 回调接口
* @author Administrator
*
*/
public static interface OnItemClickListener{
void onItemClick(String item , int position);
}

}


然后,在对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

pop.setFocusable(true)


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

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

//设置外部可以点击
pop.setOutsideTouchAble(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的)
pop.setBackgroundDrawable(new BitmapDrawable());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: