【发布时间】: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