来自PopupWindow的Android ListView的setOnItemClickListener未被调用

时间:2012-06-28 11:25:18

标签: java android popupwindow

我正在尝试从PopupWindow中显示ListView。但是当我尝试调用ListView的setOnItemClickListener时,没有任何问题可以解决。这是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);

        }
    });
}

}

这是第一个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>

这里膨胀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>

我做错了什么?

由于

3 个答案:

答案 0 :(得分:40)

只需对您的代码进行一次小修改,BOOOM您的代码就会收听您点击点击事件

final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);

您忘记在 PopupWindow 构造函数中提及可聚焦设置 true

答案 1 :(得分:5)

有同样的问题,但在我的情况下setFocusble(false)是必需的(并且在我的情况下使用ListPopupWindow不是解决方案,因为项目中的很多内容已经使用了基础PopupWindow的功能包括扩展)。

如果处于相同情况的某人有基于错误讨论的某种解决方法here(第9条)

主要思想是ListView的层次结构仍然接收触摸事件,因此我们可以手动触发onItemClick()

然而,这种方法与真正的ListView触摸处理并不是100%完全相同(就像点击一行时没有选择的亮点一样),这对我来说非常好。

如果某人有更准确的解决方案,请分享。

所以,这里是完整的Adapter代码,可以与ListView PopupWindow setFocusable(false)内的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; } 一起使用:

私有类CustomAdapter扩展了ArrayAdapter {

{{1}}

}

答案 2 :(得分:0)

愿这对你有所帮助

声明listview并在按钮ClickListener之外列出onClickListener。