如何在自定义 ListView 中从 PopupMenu 获取项目的位置

7
这是问题所在:我有一个自定义的ArrayAdapter(覆盖了getView方法)。每个item都有2个ImageView和2个EditText。其中一个ImageView是可点击的,并启用了一个PopupMenu,因此我为列表中的每个item都创建了一个小PopupMenu。现在,对于该菜单,我需要一些来自该item的参数作为锚点。那么,如何将信息(如位置)传递给从PopupMenu调用的方法呢?请参考附带的xml文件。
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/firstLine"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:maxHeight="60dp"
            android:text="Title"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:contentDescription="TODO"
            android:onClick="showMenu"
            android:src="@drawable/abc_ic_menu_moreoverflow_normal_holo_light" />

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/firstLine"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Author"
            android:textSize="12sp" />

    </RelativeLayout>

</LinearLayout>

menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >

        <item
            android:id="@+id/preview"
            android:onClick="preview"
            android:title="@string/preview"/>
        <item
            android:id="@+id/download"
            android:onClick="download"
            android:title="@string/download"/>

    </menu>

CustomArrayAdapter.java

public class CustomArrayAdapter extends ArrayAdapter<Custom>{

    private final Context context;
    private final List<Custom> values;
    private final int resource;
    private final ImageCache imgCache = new ImageCache();

    public CustomArrayAdapter(Context context, int resource, List<Custom> values) {
        super(context, resource, values);
        this.context = context;
        this.values = values;
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        Custom Custom = values.get(position);
        if (convertView == null) {
            vh = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resource, parent, false);
            vh.title = (TextView) convertView.findViewById(R.id.firstLine);
            vh.author = (TextView) convertView.findViewById(R.id.secondLine);
            vh.albumImage = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(vh);
        }
        else {
            vh = (ViewHolder)convertView.getTag();
        }
        vh.requestedCustom = Custom;
        vh.title.setText(Custom.getTitle());
        vh.author.setText(Custom.getAuthor());
        String imgUrl = Custom.getImgUrl();
        if (imgUrl != null) {
            imgCache.getImage(Custom, vh);
        }
        else {
            vh.albumImage.setImageResource(R.drawable.no_album);
        }
        return convertView;
    }

    // This is a ViewHolder that helps in the view recycling
    public static class ViewHolder {
        public Custom requestedCustom;
        public TextView title;
        public TextView author;
        public ImageView albumImage;
    }
}

"预览"和"下载"方法需要与列表项相关的参数,我不知道如何获取它们。再次感谢。
编辑1: 似乎应该使用上下文菜单(对吗?),但我想将其锚定到每个项目作为一种弹出式菜单。那么我该怎么做呢? 对于那些不理解我的意思的人(因为我的英语太差了):Google Play应用程序中,弹出菜单让您选择“安装”或“添加到愿望清单”。"

这完全取决于你如何构建你的视图。我们真的需要适配器代码和你定义的任何View类。如果你使用了任何总线,请也告诉我们(默认为否)。 - Snicolas
你如何管理图像视图上的点击?我看不到任何监听器?还没有吗? - Snicolas
@Snicolas,menu_item.xml中有一个onClick标签...因此在另一个活动中声明了两种方法。问题是它们实际上什么也没做,因为我不知道如何从弹出菜单所锚定的项目中获取一些参数。 - Angelo
4个回答

8
您可以像这样创建您自己的监听器类:
class myClickListener implements OnClickListener, OnMenuItemClickListener {

        long id;

        public myClickListener(long id) {
            this.id = id;
        }
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(appContext, v);
            popup.setOnMenuItemClickListener(this);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_item, popup.getMenu());
            popup.show();
        }
        @Override
        public boolean onMenuItemClick(MenuItem arg0) {
            Toast.makeText(appContext, "ID of selected row : " + id , Toast.LENGTH_SHORT).show();
            return false;
        }

    }

在适配器类的 getView 方法中重写此监听器并将其添加到您的 ImageView 上:

imageView.setOnClickListener(new myClickListener(getItemId(position)));

4

我们需要将按钮标签设置为其位置,并在PopupButton上单击时获取视图:

button.setTag(position);  //may be you can call this on getview method of list view
button.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {
        int position = (Integer) v.getTag();   // you will get the position

    }
});

或者在你的片段中使用接口实现 View.OnClickListener
private void showPopupMenu(View view) {

    final   int position = (Integer) view.getTag();

        PopupMenu popup = new PopupMenu(getActivity(), view);

        // Inflate our menu resource into the PopupMenu's Menu
        popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

        // Set a listener so we are notified if a menu item is clicked
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                case R.id.menu_remove:


                System.out.println(position);
                    return true;
                }
                return false;
            }
        });

        // Finally show the PopupMenu
        popup.show();
    }

1
这是我是如何做到的:

  1. 在CustomArrayAdapter的getView(int position, View convertView, ViewGroup parent)方法中,我将ViewHolder存储在convertView的标签中,而是直接存储了Custom对象本身,甚至没有使用ViewHolder。

    convertView.setTag(getItem(position));
    

    (如果你真的需要存储ViewHolder,可以使用setTag(int key, Object tag)来存储多个标签)

  2. 当ImageView调用showMenu(View view)时,我使用view.getParent()获取当前选定视图的父LinearLayout,该视图在其标记中具有我的Custom对象。

    View parent = (View) view.getParent();
    
  3. 在showMenu()之外,我定义了一个公共属性来存储当前选择的项目,如下所示:

    public static Custom mSelectedItem;
    
  4. 在showMenu()内部,我将mSelectedItem设置为步骤2中父LinearLayout的标记。

    mSelectedItem = (Custom) parent.getTag();
    
  5. 最后,在onMenuItemClick(MenuItem menuItem)覆盖方法中,我使用mSelectedItem来确定当前选择的项。

    Custom custom = mSelectedItem;
    

1

使用XML声明的onClick无法实现您想要的功能。解决您当前实现的最简单方法是:

  • 在充气后,在适配器中为ImageView添加一个真正的点击监听器。
  • 该监听器应该是一个实现OnClickListener接口的自定义类。
  • 您还应该为监听器添加一个额外的数据成员:包含ImageView的位置(行)。
  • 当监听器被激活时,它将调用活动上的回调来显示对话框,然后监听器将传递位置信息。

有没有其他替代方案(更简单更好)?你看过最新版的Google Play吗?有一个listView显示所有应用和一个弹出菜单显示列表中每个项目。菜单选项有“安装”或“添加到愿望清单”。我怎样才能实现这个功能呢? - Angelo
1
公交车?那是一个首字母缩略词吗? - Angelo
谢谢你的回答。顺便说一下,在“菜单”Android文档中:弹出菜单中的操作不应直接影响相应的内容,这就是上下文操作的作用。也许我应该使用上下文菜单而不是你建议的做法? - Angelo
你能否提供一个示例呢? - Angelo
这里有太多的选项。尝试自己解决,如果遇到障碍,那就提出一个新问题。这就是 StackOverFlow 的工作方式... - Snicolas
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接