java - 未调用来自 PopupWindow 的 Android ListView 的 setOnItemClickListener

标签 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文件进行inflate ma​​in.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>

我做错了什么?

谢谢

最佳答案

只要对你的代码做一点小改动,BOOOM 你的代码就会监听你的列表点击事件

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

您忘记在 PopupWindow 构造函数中提及 focusable 设置 true

关于java - 未调用来自 PopupWindow 的 Android ListView 的 setOnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11243564/

相关文章:

android - PopupWindow 背景有时会变得透明和紫色

java - Prim的算法在Java中的实现

java - hadoop流中的mapred.local.dir错误

java - 在 Struts 2 中使用嵌套域对象进行 CRUD 的正确方法是什么?

android - 图层列表背景不适用于 Android Lollipop (5.1.1/API 22) 上的水平 ProgressBar

java - Android N + mp4Parser 错误

java - 如何匹配双引号中的值?

android - 将图像插入 Android 中的 Realm 数据库

css - 在没有JavaScript的情况下将弹出窗口定位在屏幕中间

Android Nougat PopupWindow showAsDropDown(...)重力不起作用