Android中 PopupWindow 与 ListView 结合使用

 ArrayList	monthList = new ArrayList();
		for (int i = 0; i < monthInt; i++) {
			monthList.add((i + 1) +" "+ "月");

		}
		//反转集合
		Collections.reverse(monthList);
		tv_month.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {						
				showPopupWindow(ll_data);		
			}
		});




protected void showPopupWindow(LinearLayout view) {
		//加载布局
		layout = (LinearLayout) LayoutInflater.from(AttendanceMonthActivity.this).inflate(R.layout.dialog, null);
		//找到布局的控件
		listView = (ListView) layout.findViewById(R.id.lv_dialog);
		final String[]  postDataArrays = (String[]) monthList.toArray(new String[monthList.size()]);
		//设置适配器
		listView.setAdapter(new ArrayAdapter(AttendanceMonthActivity.this,R.layout.text, R.id.tv_text, postDataArrays));
		// 实例化popupWindow
		popupWindow = new PopupWindow(layout, DensityUtils.dp2px(getApplicationContext(), 100),LayoutParams.WRAP_CONTENT);
		//控制键盘是否可以获得焦点
		popupWindow.setFocusable(true);
		//设置popupWindow弹出窗体的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable(null,""));
		popupWindow.showAsDropDown(view,0,5);
		
		//监听
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) {
				//关闭popupWindow
				popupWindow.dismiss();
				popupWindow = null;	
				String monthChoise =postDataArrays[arg2];				
				tv_month.setText(monthChoise);
				month =monthChoise.split(" ")[0];
				atandenceMonthData();
				initDatas();
				
			
			}
		});
	}
	
	
	



    
    







    







    
    
    



		

Android中 PopupWindow 与 ListView 结合使用_第1张图片
 
  

你可能感兴趣的:(Android基础)