Android ListView的setOnItemClickListener from PopupWindow未调用

2022-09-03 16:47:06

我正在尝试从PopupWindow显示ListView。但是当我尝试调用ListView的集合OnItemClickListener时,没有什么可以haapen的。这里它是Java文件

弹出窗口窗动.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>

我做错了什么?

谢谢


答案 1

只需对代码进行一次微小的更改,BOOOM,您的代码将侦听您的列表单击事件

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

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


答案 2

有同样的问题,但在我的情况下是必需的(在我的情况下,使用不是一个解决方案,因为项目中的很多东西已经使用了base的功能,包括扩展)。setFocusble(false)ListPopupWindowPopupWindow

如果有人处于相同的情况,则有一种基于此处错误讨论的解决方法(帖子#9)

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

然而,这种方法并不是100%等同于真正的触摸处理(就像点击一行时没有选择的光芒),这对我来说目前做得很好。ListView

如果有人对这个问题有更精确的解决方案,请分享。

所以,这里是完整的代码,可以与内部一起使用,它是:AdapterListViewPopupWindowsetFocusable(false)

私有类 CustomAdapter extends 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;
}

}


推荐