【问题标题】:How to get the position of an Item from a PopupMenu in a custom ListView如何从自定义 ListView 中的 PopupMenu 获取项目的位置
【发布时间】:2013-11-15 13:19:10
【问题描述】:

问题是:我有一个自定义的 ArrayAdapter(覆盖 getView)。每个项目都有

  • 2 图像视图
  • 2 编辑文本

其中一个 ImageView 是可点击的,并启用了 PopupMenu,因此我为列表中的每个项目提供了一个小的 PopupMenu。现在,对于那个菜单,我需要一些从项目到锚定的参数。那么,如何将信息(如位置)传递给从 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 在 menu_item.xml 中有一个 onClick 标记......所以在另一个活动中有两个方法声明了这些方法。问题是他们实际上什么都不做,因为我不知道如何从 popupMenu 所锚定的项目中获取一些参数。

标签: android xml listview popupmenu


【解决方案1】:

你可以像这样创建你的监听器类:

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.setOnClickListener(new myClickListener(getItemId(position)));

【讨论】:

    【解决方案2】:

    我们只需要将buttons标签设置为它的位置,然后点击你需要的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();
        }
    

    【讨论】:

      【解决方案3】:

      没有办法通过 XML 声明的 onClick 来实现您想要的。用您的实际实现解决它的最简单方法是:

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

      【讨论】:

      • 是否有替代(更简单、更好)的实现方式?您看过最新版本的 Google Play 吗?有包含所有应用程序的 listView 和列表中每个项目的 popupMenu。菜单选项是“安装”或“添加到愿望清单”。我怎样才能做到这一点?
      • 有更简单的解决方案,但它们涉及使用总线。我描述的方式并不难,比熟悉公共汽车要容易。
      • 一辆公共汽车?是缩写吗?
      • 不,这是一种众所周知的设计模式。面向 Android 的实现包括 EventBus、Otto 或 RoboGuice。
      • 感谢您的回答。顺便说一句,来自“菜单”Android 文档:弹出菜单中的操作不应该直接影响相应的内容——这就是上下文操作的用途。也许我应该使用上下文菜单而不是按照您的建议进行操作?
      【解决方案4】:

      我是这样做的:

      1. 我没有在 CustomArrayAdaptergetView(int position, View convertView, ViewGroup parent) 方法中的 convertView 的标签中存储 ViewHolder,而是存储了自定义对象本身.我什至没有使用支架。

        convertView.setTag(getItem(position));
        

        (如果确实需要存储ViewHolder,可以使用setTag(int key, Object tag) 存储多个标签)

      2. 当 ImageView 调用 showMenu(View view) 时,我使用 view.getParent() 来获取当前选定视图的父 LinearLayout,它的标签中有我的自定义对象。

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

        public static Custom mSelectedItem;
        
      4. 在 showMenu() 内部,我从第 2 步将 mSelectedItem 设置为父 LinearLayout 的 getTag()。

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

        Custom custom = mSelectedItem;
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2021-05-12
        • 2013-09-28
        • 2015-02-16
        • 1970-01-01
        • 1970-01-01
        • 2015-05-18
        相关资源
        最近更新 更多