Android自定义系列(3)——交互式顺序选择PopupWindow

这是一个可交互的自定义PopupWindow,点击ListView的一个item后,弹出PopupWindow,选择PopupWindow里的数字顺序,PopupWindow消失,选择的数字将填写到对应item中。

之后点击下一个item,弹出的PopupWindow中已经选择过的数字将消失。


Android自定义系列(3)——交互式顺序选择PopupWindow_第1张图片


1.自定义一个SpinnerPopup类,继承PopupWindow,在其中添加一个内部适配器类,适配SpinnerPopup中的ListView数据。


public class SpinnerPopup extends PopupWindow{
	private Context mContext;
	private View mView;
	private ListView listView;
	private Button btn_left;
	private ArrayList strList;
	//ListView中TextView中的值
	public String no;
	//顺序数字
	public int spinnerPosition;
	
	public SpinnerPopup(View view,Context context,ArrayList list){
		super(view,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
		this.mView = view;
		this.mContext = context;
		this.strList = list;
		listView=(ListView)mView.findViewById(R.id.spinner_listView);
		btn_left = (Button) mView.findViewById(R.id.btn_client_left);
		ListViewAdapter adapter = new ListViewAdapter(context,strList);
        listView.setAdapter(adapter);
        //listView点击item触发事件
        listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView parent, View view, int position,
					long id) {
				TextView textView = (TextView) view.findViewById(R.id.tv_spinner);
				no = textView.getText().toString();
				//将选择的数字赋值到顺序数字全局变量
				spinnerPosition = position;
				//关闭popupWindow
				SpinnerPopup.this.dismiss();
			}
		});
        
        btn_left.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				//点击取消按钮将全局变量设为-1,为Activity中的监听popupWindow消失的触发提供判断依据
				spinnerPosition = -1;
				SpinnerPopup.this.dismiss();
				
			}
		});
	}
	
	public String getNo(){
		return no;
	}
	
	public int getSpinnerPostion(){
		return spinnerPosition;
	}
	//listView的数字数据适配器
	class ListViewAdapter extends BaseAdapter{
		private ArrayList sList;
		
		public ListViewAdapter(Context context, ArrayList list){
			sList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return sList.size();
		}
	
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return sList.get(position);
		}
	
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
	
		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			LayoutInflater layoutInflater = LayoutInflater.from(mContext);
			convertView = layoutInflater.inflate(R.layout.spinner_item, null);
			if(convertView!=null){
				TextView textView = (TextView) convertView.findViewById(R.id.tv_spinner);
				
				textView.setText(sList.get(position).toString().trim());
			}
			return convertView;
		}
	}
}


2.在showPopup方法中显示PopupWindow,并设置PopupWindow的消失监听,PopupWindow消失后,将选择的数据更新到指定的item的子控件中。


	public void showPopup(){
		View spinnerPopup_view=mInflater.inflate(R.layout.spinner_popup, null, false);	
		final SpinnerPopup sinnerpop=new SpinnerPopup(spinnerPopup_view,context,listDate);
		
		sinnerpop.setWidth(200);
		//设置popupWindow消失的监听事件
		sinnerpop.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				spinnerPosition = sinnerpop.getSpinnerPostion();
				if(spinnerPosition!=-1){
					no = sinnerpop.getNo();
					//通过Dao将选择的数字存入数据库中
					clientDao.update(client.getOrder_from_no(), no);
					changeTextToTextView(listView, listViewPosition, R.id.text_order_no);
				}
				
			}
		});
		sinnerpop.showAtLocation(mView, Gravity.CENTER, 0, 0);
	}


3.这是将选择的数据更新到指定item的子控件中的方法,是根据item的position来获取item的view控件,再通过view来获取当中的子控件。


	/**
	 * 更新listView中的TextView的值
	 * @param listView
	 * @param position
	 * @param resId
	 */
	public void changeTextToTextView(ListView listView, int position,  
	        int resId) {  
		//根据position获取listView中的item控件
	    View view = listView.getChildAt(  
	            position - listView.getFirstVisiblePosition());  
	    //在item中获取子控件TextView
	    TextView textView = (TextView) view.findViewById(resId);
	    textView.setText(no);
	    listDate.remove(spinnerPosition);
	}


/**
	 * 更新listView中所有TextView的值
	 * @param listView
	 * @param position
	 * @param resId
	 */
	public void changeAllTextToTextView(ListView listView, int position,  
	        int resId) {  
	    View view = listView.getChildAt(  
	            position - listView.getFirstVisiblePosition());  
	    TextView textView = (TextView) view.findViewById(resId);
	    textView.setText("请填写顺序");
	}

4.这是重置数据的方法,可触发调用它进行重置。


	/**
	 * 重置Listview和SpinnerPopup中的数据
	 */
	public void reset(){
		listDate.clear();
		for(int i=0;i


5.这是Listview的点击item触发事件,获取当前点击item的position,赋值到全局变量


			//点击listView中的item,触发弹出popupWindow
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView parent, View view,
						int position, long id) {
					//获取当前点击item的下标,作用于更新listView中的TextView
					listViewPosition = position;
					if(listDate.size()!=0){
						//获取当前选择item的数据,作用于更新数据库,client是一个数据持久化类,一个Bean
						client = clientList.get(position);
						showPopup();
					}
						
				}
			});


6.spinner_popup布局文件,这个布局文件在showPopup方法中实例化SpinnerPopup时作为参数传入。





    


7.spinner_popup布局文件的圆角样式。


    

     
     
     

你可能感兴趣的