我有一个完美的自定义列表视图。 现在,我希望列表中的每个项目都有一个下拉菜单,这样当用户点击列表中的任何项目时,下拉菜单会显示两个选项 - 编辑和删除。根据用户的选择,列表视图项目将被处理。
我想知道将下拉菜单添加到每个listview项的方法。
列表项的xml文件 -
theano.function([index],classifier.errors(y),
gives={
x:(trainset_x[0][index*batch:(index+1)*batch],trainset_x[1][index*batch:(index+1)*batch)
y:trainset_y[index*batch:(index+1)*batch]},allow_input_downcast=True)
适配器类
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="120dp"
android:background="#FFFFFF">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:id="@+id/chkitem"
android:layout_alignParentLeft="true"
/>
<TextView android:id="@+id/textTitle"
android:layout_width="200dp"
android:layout_height="60dp"
android:textSize="15dp"
android:textColor="#000000"
android:textStyle="bold"
android:gravity="fill"
android:layout_toRightOf="@+id/chkitem"
/>
<TextView
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/detail"
android:textSize="15dp"
android:textStyle="italic"
android:gravity="fill"
android:layout_toRightOf="@+id/chkitem"
android:layout_below="@+id/textTitle"/>
</RelativeLayout>
我希望它是这样的 -
答案 0 :(得分:18)
检查:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#FFFFFF">
<CheckBox
android:id="@+id/chkitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/textTitle"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_toRightOf="@+id/chkitem"
android:gravity="fill"
android:textColor="#000000"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="@+id/detail"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="@+id/textTitle"
android:layout_toRightOf="@+id/chkitem"
android:gravity="fill"
android:textSize="15dp"
android:textStyle="italic" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />// Replace Image
</RelativeLayout>
适配器类:
public class list_addr_adapter extends ArrayAdapter<list_addr> {
Context context;
int layoutResourceId;
public list_addr_adapter(Context context, int layoutResourceId, List<list_addr> items) {
super(context, layoutResourceId, items);
this.layoutResourceId = layoutResourceId;
this.context = context;
// this.listener=callback;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
TextView txtDetail;
ImageView imageview;
}
ViewHolder holder = null;
public View getView(int position, View convertView, ViewGroup parent) {
final list_addr lists = getItem(position);
final int pos=position;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.items_sav_addr2, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle);
holder.txtDetail = (TextView) convertView.findViewById(R.id.detail);
holder.imageview = (ImageView) convertView.findViewById(R.id.imageview);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(lists.getTitle());
holder.txtDetail.setText(lists.getDetail());
try {
holder.imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageview:
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
popup.getMenuInflater().inflate(R.menu.clipboard_popup,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
//Or Some other code you want to put here.. This is just an example.
Toast.makeText(getApplicationContext(), " Install Clicked at position " + " : " + position, Toast.LENGTH_LONG).show();
break;
case R.id.delete:
Toast.makeText(getApplicationContext(), "Add to Wish List Clicked at position " + " : " + position, Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
});
break;
default:
break;
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
弹出menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/install"
android:title="Install" />
<item
android:id="@+id/addtowishlist"
android:title="Add to wishlist" />
</menu>