PopupWindow+ListView+OnItemClick无效
代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// 生成ListView
ListView listview = new ListView(MainActivity.this);
String strs = new String;
strs = "asfsafdasdf";
strs = "桂 地要仍地沃尔夫仍";
strs = "asfsafdasdf";
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(MainActivity.this,R.layout.common_poplist_item_single_textview,R.id.content,strs);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.common_poplist_item_single_textview, strs);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
System.out.println(position);
}
});
PopupWindow window = new PopupWindow(MainActivity.this);
// 设置宽高
window.setWidth(200);
window.setHeight(120);
// 填充
window.setContentView(listview);
// 外部可部
window.setOutsideTouchable(true);
// 加了下面这行,onItemClick才好用
window.setFocusable(true);
window.showAsDropDown(button1);
}
});
}