Android 通过长按列表视图项目创建弹出对话框

Android creating a popup dialog by long click on listview item

我有点困惑如何处理长按单独的列表视图项目,然后让该项目显示弹出窗口。

所以这是我的代码,如果您有不明白的地方,请提出一些问题。

MainActivity.java

ListView listView = (ListView) findViewById(R.id.listView);

listView.setOnLongClickListener(new View.OnLongClickListener() {
            @SuppressLint("ResourceType")
            @Override
            public boolean onLongClick(View v) {
                PopupMenu p = new PopupMenu(MainActivity.this, v);
                p.getMenuInflater().inflate(R.layout.popup_layout, p.getMenu());
                p.show();
                return true;
            }
        });

popup_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <menu
        android:layout_width="185dp"
        android:layout_height="141dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="96dp"
        tools:layout_editor_absoluteY="83dp">

        <item
            android:id="@+id/order_takeout"
            android:onClick="doTakeOut"
            android:title="@string/order_takeout"
            android:layout_width="delete" />

        <item
            android:id="@+id/order_eat_in"
            android:onClick="doEatIn"
            android:title="@string/edit" />
    </menu>
</androidx.constraintlayout.widget.ConstraintLayout>

viewcontents_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp"
        android:longClickable="true"
        tools:ignore="MissingConstraints"/>

</androidx.constraintlayout.widget.ConstraintLayout>

如果有人能帮助我,我很高兴得到答案。 保持健康!

你的错误是在使用setOnLongClickListener 你可以使用:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {


            return true;
        }
    }); 

您必须在 ListView 中设置 setOnItemLongClickListener() 如何在列表视图上实现长按监听器

所以我解决了这个问题。

  1. 使用另一个名为 ViewListContents 的 java class 并更改了我的位置popup_layout 是,我把它移到菜单中并设置了一些菜单标签而不是约束标签。

  2. 我已将 onItemLongClickListener

    中的 R.layout 更改为 R.menu

在这个class中我是这样使用的:

    listView = (ListView) findViewById(R.id.listView);

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
            PopupMenu p = new PopupMenu(ViewListContents.this, v);
            MenuInflater inflater = p.getMenuInflater();
            inflater.inflate(R.menu.popup_layout, p.getMenu());
            p.show();
            return true;
        }
    });