[英]Android ListView's setOnItemClickListener from PopupWindow not called
I'm trying to show a ListView from a PopupWindow. 我正在尝试从PopupWindow中显示ListView。 but when I'm try to call ListView's setOnItemClickListener nothing to haapen.
但是当我尝试调用ListView的setOnItemClickListener时,没有任何问题可以解决。 Here It Java file
这是Java文件
PopupWindowActivity.java PopupWindowActivity.java
public class PopupWindowActivity extends Activity {
String[] data = { "DATA 1", "DATA 2", "DATA 3", "DATA 4", "DATA 5", "DATA 6" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.main, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ListView listView = (ListView) popupView.findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,data));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("Item Clicked");
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 20, -5);
}
});
}
} }
Here it is first xml file a.xml 这是第一个xml文件a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/openpopup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Popup Window" />
</LinearLayout>
Here it inflate xml file main.xml 这里它膨胀xml文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/recording"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="30sp"
android:layout_marginLeft="30sp"
android:layout_marginRight="30sp"
android:layout_marginTop="100sp" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000" >
</ListView>
</RelativeLayout>
</LinearLayout>
What am I doing wrong? 我究竟做错了什么?
Thanks 谢谢
Just one minor change in your code and BOOOM your code will listen to you list click event 只需对代码进行一次小改动,BOOOM您的代码就会收听列表点击事件
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
You forget to mention focusable setting true in PopupWindow constructor. 你忘了在PopupWindow构造函数中提到可聚焦设置为true 。
had the same problem, but in my case setFocusble(false)
was required (and using ListPopupWindow
was not a solution in my case as a lot of stuff in the project already used base PopupWindow
's functionality including extending). 有同样的问题,但在我的情况下需要
setFocusble(false)
(并且在我的情况下使用ListPopupWindow
不是解决方案,因为项目中的很多东西已经使用了PopupWindow
的基本功能,包括扩展)。
If someone in the same situation there is a kind of workaround based on bug discusson here (post #9) 如果有人在同样的情况有基于错误商榷的一种变通方法这里 (职位#9)
The main idea is that ListView
's hierarchy is still receives touch events so we can manually trigger onItemClick()
. 主要思想是
ListView
的层次结构仍然接收触摸事件,因此我们可以手动触发onItemClick()
。
However this approach is not 100% identical to real ListView
's touch handling (like there is no glow of selection while tapping a row) this done pretty well for me for the moment. 然而,这种方法与真正的
ListView
的触摸处理并不是100%相同(就像点击一行时没有选择的光芒)这对我来说非常好。
If someone has more precise solution of this problem, please share. 如果有人有更精确的解决方案,请分享。
So, here is complete Adapter
's code which can be used with ListView
inside PopupWindow
which is setFocusable(false)
: 所以,这里是完整的
Adapter
代码,可以与PopupWindow
中的ListView
一起使用,它是setFocusable(false)
:
private class CustomAdapter extends ArrayAdapter { 私有类CustomAdapter扩展ArrayAdapter {
private LayoutInflater mInflater;
private ListView mOwningListView;
public CustomAdapter(Context context, List<String> objects, ListView listView) {
super(context, android.R.layout.simple_list_item_1, objects);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOwningListView = listView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.font_pick_row, null);
}
// this is the key point of workaround
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*
* as every row is still receiving their touches
* we can use this to manually trigger onItemClick
* since it doesn't firing in popupWindow.setFocusable(false)
*/
mOwningListView.getOnItemClickListener().onItemClick(mOwningListView, v, position, getItemId(position));
}
});
//... other stuff
return convertView;
}
} }
May this help you . 愿这对你有所帮助。
Declare listview and list onClickListener outside the button ClickListener. 声明listview并在按钮ClickListener之外列出onClickListener。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.