popupWindow 的使用方法

这段时间一直在玩PopupWindow,前段时间做了一个仿淘宝旅行日期选择的上拉PopupWindow 的小demo,但是其日期选择我没有实现,这个还是有一定的难度,我只是实
现了其动画效果,做这个东西做了我两天,由于对PopuWindow不是很熟悉,再加上被一个粗心的错误困恼了一段时间(我发现大部分的bug都是来自于程序员的自我粗心,而不是

代码的逻辑错误,我觉得代码的逻辑错误才算的上是真正的bug) ,以及对PopuWindow中ListView的监听掌握的不是很好,也浪费了一段时间,不过最终还是做出来了自己想要

的效果。我们先来说一下PopupWindow的基本用法吧,然后再上图,上代码:

一:popupWindow 的初始化:

	popupWindow = new PopupWindow(view, displayWidth, displayHeight);

view 为popupWindow的样式,这个样式可以是自定义的样式,也可以就是一个Button,displayWidth 为显示的宽度,displayHeight为显示的高度

二:设置在屏幕外也能够点击消失:

	popupWindow.setOutsideTouchable(true);

三:设置点击返回键也能够消失:

	popupWindow.setFocusable(false);

如果是设置为false 那么你这样设置也没用

	popupWindow.setBackgroundDrawable(new BitmapDrawable());

因为popupWindow无法获取焦点,你设置背景也没有用,但是如果你设置了popupWindow.setOutsideTouchable(true); 则照样能够点击外面消失,如果没有设置则无法消失

点击返回键也无法消失。所以要想点击返回键是popupWindow消失则必须两则同时设置:

	popupWindow.setFocusable(true);
	popupWindow.setBackgroundDrawable(new BitmapDrawable());


四:返回键失效:

如果你这样进行设置返回键会失效:如果你将setFocusable 设置为true时则不会失效

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 刷新状态
		popupWindow.update();

但是如果你设置了背景之后,返回键又不会失效:

		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.update();

五:showAsDropDown() 这个方法在popupWindow中有两个个被重写,showAsDropDown(View anchor, int xoff, int yoff),showAsDropDown(View anchor),

anchor 参数为这个popupWindow所依附的控件,其会在这个空间下显示出来,而不用设置popupWindow的位置。,x,y 分别为偏移的位置

六:showAtLocation(View parent, int gravity, int x, int y),这个函数没有依赖具体控件的位置,而是在可以再屏幕上设置其显示的位置,parent 为popupWindow所获取的屏幕

控制的父控件:API是这样介绍的:a parent view to get the getWindowToken() token from, gravity为设置在屏幕显示的大致位置,是居中还是居左还是居右等等,x,y 则

是设置其在屏幕开始的显示位置(记住屏幕的原点坐标是在屏幕的左上角,朝下的为Y正轴)

这样popupWindow的使用方法基本上就介绍完了,下面上代码和图:

          popupWindow 的使用方法_第1张图片             popupWindow 的使用方法_第2张图片

由于不是在一台手机上截的图,所以大小不一样,我做的还没有去调整颜色,按钮什么的所以就将就着吧,这个可以点击进行选择。

下面上代码吧,首先写适配器:我的是继承了BaseAdapter

public class CancelTaskListAdapter extends BaseAdapter{

	Context context;
	String causeArray[];
	private OnClickListener onClickListener;
	private OnItemClickListener mOnItemClickListener;
	public CancelTaskListAdapter(Context context,String causeArray[]){
		this.context = context;
		this.causeArray = causeArray;
		init();
	}
	@Override
	public int getCount() {
		return causeArray.length;
	}

	@Override
	public Object getItem(int arg0) {
		return causeArray[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			LinearLayout ll = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.cancel_task_item, null);
			convertView = ll;
			TextView cause = (TextView)convertView.findViewById(R.id.cause);
			ImageView select = (ImageView)convertView.findViewById(R.id.select);			
			cause.setText(causeArray[position]);
			if(MainActivity.positioncause.equals(causeArray[position])){
				select.setBackgroundResource(R.drawable.icon_check);
			}		
			ViewHold hold = new ViewHold();
			hold.setCause(cause);
			hold.setSelect(select);
			hold.setPosition(position);
			convertView.setTag(hold);
			
		}else{
			ViewHold hold = (ViewHold)convertView.getTag();
			TextView cause = hold.getCause();
			ImageView select = hold.getSelect();
			hold.setPosition(position);
			convertView.setTag(hold);
			if(select.getResources()!=null){
				select.setBackgroundResource(0);
			}
			if(MainActivity.positioncause.equals(causeArray[position])){
				select.setBackgroundResource(R.drawable.icon_check);
			}
			cause.setText(causeArray[position]);			
		}
		convertView.setOnClickListener(onClickListener);
		return convertView;
	}
	public void setOnItemClickListener(OnItemClickListener listener) {
		mOnItemClickListener = listener;
	}
	/**
	 * 重新定义菜单选项单击接口
	 */
	public interface OnItemClickListener {
		public void onItemClick(View view,int position);
	}
	public void init(){
		onClickListener = new OnClickListener(){			
			@Override
			public void onClick(View v) {
				if(mOnItemClickListener!=null){
					ViewHold hold = (ViewHold)v.getTag();
					int num = hold.getPosition();
					mOnItemClickListener.onItemClick(v,num);
					notifyDataSetChanged();
				}
			}
		};
	}
	class ViewHold {
		private TextView cause;
		private ImageView select;
		private int position;
		
		public int getPosition() {
			return position;
		}

		public void setPosition(int position) {
			this.position = position;
		}

		public TextView getCause() {
			return cause;
		}

		public void setCause(TextView cause) {
			this.cause = cause;
		}

		public ImageView getSelect() {
			return select;
		}

		public void setSelect(ImageView select) {
			this.select = select;
		}
	}
}

然后是按钮的监听,ListView的监听:

public class MainActivity extends Activity implements OnClickListener{

	public static String positioncause = null;
	String cancelArray[] = {"另约时间地点","客户要求销案","任务已改派","联系不上","请示改派","已去远程定损点","其它"};
	Button cancel;
	PopupWindow popupWindow;
	ListView cancelCauseList;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		cancel = (Button)findViewById(R.id.button1);
		cancel.setOnClickListener(this);
	}
	@Override
	public void onClick(View v){
		CancelTask cancelTask = new CancelTask(this, cancelArray);
		cancelTask.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v){
				switch(v.getId()){
				case R.id.cancel:
					popupWindow.dismiss();
					popupWindow = null;
					Log.i("MainActivity", "取消");
					break;
				case R.id.sure:
					Toast.makeText(MainActivity.this, positioncause, Toast.LENGTH_SHORT).show();
					cancel.setEnabled(false);
					popupWindow.dismiss();
					popupWindow=null;
					Log.i("MainActivity", "确定");
					break;
				}
			}
		});
	}
	class CancelTask{
		Context context;
		String causeArray[];
		TextView canceltv,sure;
		int displayWidth;
		int displayHeight;
		View view;
		CancelTaskListAdapter adapter;
		public CancelTask(Context context,String causeArray[]){
			this.context = context;
			this.causeArray = causeArray;
			init();		
		}	
		public void showPopupWindow(){
			popupWindow.showAtLocation(cancel, Gravity.BOTTOM,displayWidth/2,0);
		}
		
		public void hidePopupWindow(){
			if(popupWindow!=null&&popupWindow.isShowing()){
				popupWindow.dismiss();
				popupWindow = null;
			}
		}		
		public void setOnClickListener(OnClickListener listener){
			canceltv.setOnClickListener(listener);
			sure.setOnClickListener(listener);			
		}
		public void init(){
			view = LayoutInflater.from(context).inflate(R.layout.cancel_task, null);
			cancelCauseList = (ListView)view.findViewById(R.id.list_cause);
			canceltv = (TextView)view.findViewById(R.id.cancel);
			sure = (TextView)view.findViewById(R.id.sure);
			
			adapter = new CancelTaskListAdapter(context, causeArray);
			cancelCauseList.setAdapter(adapter);
			adapter.setOnItemClickListener(itemClickListener());
			displayWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
			displayHeight = ((Activity) context).getWindowManager().getDefaultDisplay().getHeight();
			System.out.println(displayHeight);
			System.out.println(displayWidth);
			MainActivity.positioncause = causeArray[1];
			startAnimation();
		}

		public void startAnimation(){
			if(popupWindow==null){
				popupWindow = new PopupWindow(view, displayWidth, displayHeight*3/4);
				popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
			
				popupWindow.setOutsideTouchable(true);
				popupWindow.setFocusable(true);
				popupWindow.setBackgroundDrawable(new BitmapDrawable());
//				popupWindow.update();
			}
			showPopupWindow();
		}			
		CancelTaskListAdapter.OnItemClickListener itemClickListener(){
			CancelTaskListAdapter.OnItemClickListener listener = new OnItemClickListener(){				
				public void onItemClick(View view,int position) {
					positioncause = causeArray[position];
				}
			};
			return listener;
		}		
	}
}



 

你可能感兴趣的:(popupWindow 的使用方法)