问题在于:我有一个自定义的ArrayAdapter(覆盖getView)。每个项目都有
其中一个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应用程序,弹出菜单,让您在“安装”或“添加到列表”之间进行选择。
答案 0 :(得分:7)
您可以像这样创建监听器类:
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)));
答案 1 :(得分:3)
我们只需将按钮标记设置为其位置,然后单击要获取视图所需的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();
}
答案 2 :(得分:1)
使用XML声明的onClick无法实现您想要的效果。实际实施的最简单方法是:
答案 3 :(得分:1)
以下是我的表现:
不是将ViewHolder存储在 CustomArrayAdapter 的 getView(int position,View convertView,ViewGroup parent)方法的convertView标记中,我存储了Custom对象本身。我甚至没有使用持有人。
convertView.setTag(getItem(position));
(如果你真的需要存储ViewHolder,你可以使用setTag(int key,Object tag) 存储多个标签)
当ImageView调用showMenu(视图视图)时,我使用view.getParent()来获取当前所选视图的父LinearLayout,其标签中包含我的Custom对象。
View parent = (View) view.getParent();
在showMenu()之外我已经定义了一个公共属性来存储当前选中的项目,如下所示:
public static Custom mSelectedItem;
在showMenu()中,我将mSelectedItem设置为步骤2中父LinearLayout的getTag()。
mSelectedItem = (Custom) parent.getTag();
最后,在 onMenuItemClick(MenuItem menuItem)覆盖方法中,我使用mSelectedItem来确定当前选择的项目。
Custom custom = mSelectedItem;