android - 如何使用 Android 上下文菜单(点击并按住)

标签 android listview contextmenu listadapter

我很难理解 Android 上下文菜单的工作原理。我正在编写一个简单的程序,它将在 ListView 中显示不同类型冰淇淋的列表。当用户“点击并按住每种类型时,应该会出现一个上下文菜单,其中包含 4 个选项,询问他们想要显示有关该冰淇淋的哪些信息(图片、成分、订单、其他)。

我不明白如何制作,以便每个列表项的上下文菜单提供适当的信息(与用户点击并按住的任何列表项关联的信息)。

我正在根据老师给出的示例进行工作,该示例具有我想要的一些功能,但没有处理为每个列表项显示不同信息的问题。我看到在给定的代码中,我的教授甚至在 onCreateContextMenu() 的参数内的注释中给出了提示:

public void onCreateContextMenu( //called each time the context menu is requested
        ContextMenu menu,   //the ContextMenu itself
        View v, //The view for which the context menu is being built
        ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

但我仍然不确定我能用这个参数做什么,而且我已经转了很长时间了。 stackoverflow 上至少有一个其他线程解决了这个问题,但它并没有帮助我更接近地理解这里发生的事情。

我真的很感谢你的帮助,因为我对 Android(以及一般的 OOP)还很陌生,并且有点犹豫是否要提问,因为担心公众技术人员的 mock 或被视为利用社区。我只是希望在某个时候我能够将其转给其他人。

谢谢!

这是我的代码:

package CS285.Assignment2;

import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class IceCreamMenu extends ListActivity {
    TextView selection;
    ImageView imgView;

    String[] items={"Butter Pecan", "Chocolate", 
            "Coffee", "Mango", 
            "Rocky Road","Strawberry", 
            "Vanilla"};

    int[] images={R.drawable.butterpecan,
            R.drawable.choclate,
            R.drawable.coffee,
            R.drawable.mango,
            R.drawable.rockyroad,
            R.drawable.strawberry,
            R.drawable.vanilla};

    public static final int IMG_ID = 0;
    public static final int INGR_ID = 1;
    public static final int ORDER_ID = 2;
    public static final int OTHER_ID = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection=(TextView)findViewById(R.id.selection);
        imgView = (ImageView)findViewById(R.id.Image);

        setListAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, 
                                                items));
        registerForContextMenu(getListView()); //indicate that the ListView has a context menu.

    }

    //event handling for ListItemClick events
    public void onListItemClick(ListView parent, View v,
                        int position, long id) {

        selection.setText(items[position]);

    }

    @Override
    public void onCreateContextMenu( //called each time the context menu is requested
            ContextMenu menu,   //the ContextMenu itself
            View v, //The view for which the context menu is being built
            ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

            populateMenu(menu);

    }

    private void populateMenu(Menu menu) {
        menu.add(Menu.NONE, IMG_ID, Menu.NONE, "Picture");
        menu.add(Menu.NONE, INGR_ID, Menu.NONE, "Ingredients");
        menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order");
        menu.add(Menu.NONE, OTHER_ID, Menu.NONE, "Other");

    }

    private boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case IMG_ID:

            imgView.setImageResource(images[0]);

            return(true);

        case INGR_ID:


            return(true);

        case ORDER_ID:


            return(true);

        case OTHER_ID:


            return(true);
        }

        return(false);
    }

    //called whenever an item in a ContextMenu is selected.
    @Override
    public boolean onContextItemSelected(MenuItem item) { //item is the menu item chosen
        return(applyMenuChoice(item) ||  
        super.onContextItemSelected(item));
    }


}

最佳答案

简短的答案是将 menuInfo 转换为 AdapterContextMenuInfo 对象,然后访问其公共(public)位置成员变量

selectedPostion = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;

selectedPostion的值是ListView中长按的位置。

Android 开发人员指南中的这篇文章对如何使用 ContextMenuInfo 对象进行了精彩的描述,必读:

http://developer.android.com/guide/topics/ui/menus.html#context-menu

正如您在 onCreate() 方法中看到的,您的 ListView 正在注册自身以响应长按并生成 ContextMenu。因此,当您长按 ListView 中的某个 View 项时,ListView 会为您的 onCreateContextMenu 方法提供 menuInfo 参数。此参数包含有关如何生成 ContextMenu 的信息。

http://developer.android.com/reference/android/view/ContextMenu.ContextMenuInfo.html

ListView 扩展了 AdapterView 类。其中有 AdpterView.AdapterContextMenuInfo 子类。每当长按 ListView 对象时,它都会设置此参数,其中包含有关长按哪个 View 对象的信息。然后该对象被传递给 onCreateContextMenu(...) 方法。

http://developer.android.com/reference/android/widget/AdapterView.AdapterContextMenuInfo.html

关于android - 如何使用 Android 上下文菜单(点击并按住),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4019337/

相关文章:

android - 如何使用 MOSHI 将 un json 字符串解析为列表

android - ListView 保存复选框的状态

android - 如何将 ListView 项目加载并保存到 SQLiteDatabase?

python - 如何从 PyQt 中的 QListView 中选择项目

python-2.7 - 如何在 PyQt 中为 QTableView 的每个单元格添加右键菜单

Android:Asynctask 中的多个参数

android - 将动画添加到 Android 中的 ListView

java - 如何显示与屏幕成比例的图像

javascript - 禁用 Firefox 愚蠢的右键单击上下文菜单

javascript - 通过 Chrome 扩展程序中的上下文菜单选项复制所选文本