PopWindow与ListView多选实现

效果图:

PopWindow与ListView多选实现_第1张图片

点击Button后弹出ListView


window.setFocusable(true);

设置除window外之外区域也有响应事件,此时可以响应窗口里面控件

window.setBackgroundDrawable(new BitmapDrawable());

设置窗口背景,否则只能响应Home键

关于CheckBox值获取,我使用的android自带ArrayAdapter,可以使用

long[] ids = listView11.getCheckItemIds();

来获取已经选择checkbox ids


Button 里无法再次响应点击事件,不知道是不是window的原因,我后来在

xml声明的时候加了一句onClick事件,声明一个方法来实现点击响应事件


src见附件,另外附上源码:

public class MainActivity extends Activity {
	
//	private ListView  listView;
	PopupWindow window;
	LinearLayout rl;
	Button button;
	private ListView listView11;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		listView = (ListView) findViewById(R.id.listView1);
//		rl = (RelativeLayout) findViewById(R.id.rl);
		final ArrayAdapter adapter = new ArrayAdapter(this, 
				android.R.layout.simple_list_item_multiple_choice, GENRES);
		
		rl = new LinearLayout(this);
//		
//		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//		listView.setItemsCanFocus(false);
//		listView.setAdapter(adapter);
		
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			

			private Button b1;

			@Override
			public void onClick(View v) {
				LayoutInflater factory = LayoutInflater.from(MainActivity.this);
				View v1 = factory.inflate(R.layout.selector, null);
				listView11 = (ListView) v1.findViewById(R.id.listView1);
				b1 = (Button) v1.findViewById(R.id.button1);
//				b1.setOnClickListener(new OnClickListener() {
//					
//					@Override
//					public void onClick(View v) {
//						long[] ids = listView11.getCheckedItemIds();
//						for(long id : ids) {
//							System.out.println("id0-->" + id);
//						}
//						
//					}
//				});
//				listView11.seta
				
//				listView11 = new ListView(MainActivity.this);
				listView11.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
				listView11.setItemsCanFocus(false);
				listView11.setAdapter(adapter);
//				rl.addView(listView11);
				window = new PopupWindow(v1, 120, 200);
				window.setBackgroundDrawable(new BitmapDrawable());
				window.showAsDropDown(button);
//				window.setOutsideTouchable(true);
				window.setFocusable(true);
				window.update();
				
//				listView11.setOnItemClickListener(new OnItemClickListener() {
//
//					@Override
//					public void onItemClick(AdapterView arg0, View arg1,
//							int arg2, long arg3) {
//						// TODO Auto-generated method stub
//						Toast.makeText(MainActivity.this, arg0.getAdapter().getItem(arg2) + "", 0).show();
//					}
//				});
			}
		});
		
		
//		 if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
//			 ListFragmentt list = new ListFragmentt();
//	            getFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
//	    }
	}
	public void doClickOK(View v){
		long[] ids = listView11.getCheckItemIds();
		Toast.makeText(this, "doClickOK-->" + ids[0], 0).show();
		window.dismiss();
	}
	
	class ListFragmentt extends ListFragment {
		
		/*@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			
			setListAdapter(new ArrayAdapter(getActivity(), 
					android.R.layout.simple_list_item_multiple_choice, GENRES));
			getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
			getListView().setItemsCanFocus(false);
		}
		
		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			Toast.makeText(getActivity(), l.getItemAtPosition(position) + "", 0).show();
		}*/
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
			// TODO Auto-generated method stub
			if(keyCode == KeyEvent.KEYCODE_BACK) {
				if(window != null)
					window.dismiss();
				return true;
			}
			return super.onKeyDown(keyCode, event);
	}
	
	private static final String[] GENRES = new String[] {
        "a", "b", "c", "d", "e"};

}

你可能感兴趣的:(Android)