Android-怎么对listview里的每一个item添加一个contextmenu

Android-怎么对listview里的每一个item添加一个contextmenu

晚风撩人 发布于 2017-09-17 字数 162 浏览 1229 回复 4

想将listview里的每一个item添加一个contextmenu。我用的方法是

listview.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {});

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑾兮 2017-10-16 4 楼

先给个例子,你照着试一下,有问题的话,先把你的代码贴出来。

 public class ComplexListActivity extends ListActivity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setListAdapter(new ComplexObjectAdapter(this, R.layout.item, getComplexObjects()));
registerForContextMenu(getListView());
}

private List getComplexObjects() {
List<ComplexObject> list = new ArrayList<ComplexObject>();
list.add(new ComplexObject("1", "1", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("2", "2", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("3", "3", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("4", "4", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("5", "5", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("6", "6", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("7", "7", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("8", "8", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("9", "9", getResources().getDrawable(R.drawable.icon)));
return list;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
Log.e("", "bad menuInfo", e);
return false;
}
long id = getListAdapter().getItemId(info.position);
Log.d("", "id = " + id);
Toast.makeText(this, "id = " + id, Toast.LENGTH_SHORT).show();
return true;
}

private class ComplexObjectAdapter extends ArrayAdapter<ComplexObject> implements View.OnCreateContextMenuListener {

private List<ComplexObject> objects;

public ComplexObjectAdapter(Context context, int textViewResourceId, List<ComplexObject> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.item, null);
}
final ComplexObject o = objects.get(position);
if (o != null) {

TextView textlInfo = (TextView) v.findViewById(R.id.info);
textlInfo.setText(o.getName());

ImageView channelIcon = (ImageView) v.findViewById(R.id.icon);
channelIcon.setAdjustViewBounds(true);
channelIcon.setMaxHeight(30);
channelIcon.setMaxWidth(30);
channelIcon.setImageDrawable(o.getLogo());

ImageButton button = (ImageButton) v.findViewById(R.id.button);
button.setImageResource(R.drawable.icon);
v.setOnCreateContextMenuListener(this);

}
return v;
}

public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
// empty implementation
}

}
}

泛泛之交 2017-09-24 3 楼

你是在adapter里面加的吗?或者是用listview.setOnLongClickListener(在里面创建上下文菜单)

想挽留 2017-09-23 2 楼

会不会长按的监听被别的截获了?没传到你的listview上。我记得我我的Listview就是这样添加contextmenu的

偏爱自由 2017-09-23 1 楼

记得不是很清楚了,引用下别人的回答

1.在activity的onCreate(...)方法中为一个view注册上下文菜单

2.在onCreateContextMenuInfo(...)中生成上下文菜单。

3.在onContextItemSelected(...)中响应上下文菜单项。

1)注册上下文菜单
/**
* 上下文菜单演示Demo
*
* @author CodingMyWorld 2011-8-27 下午03:22:39
*/
publicclass SampleContextMenuActivity extends ListActivity {
privatestaticfinal String TAG ="SampleContextMenuActivity";

@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 显示列表
simpleShowList();
// 为所有列表项注册上下文菜单
this.registerForContextMenu(getListView());
}

privatevoid simpleShowList() {
// list item
String[] files =new String[] {
"文件1",
"文件2",
"文件3",
"文件4"
};
// simple array adapter
ArrayAdapter<String> adapter =new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
files);
// set adapter
this.setListAdapter(adapter);
Log.v(TAG, "show simple list");
}
}

2)生成上下文菜单
在activity中重写方法。

 publicvoid onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
Log.v(TAG, "populate context menu");
// set context menu title
menu.setHeaderTitle("文件操作");
// add context menu item
menu.add(0, 1, Menu.NONE, "发送");
menu.add(0, 2, Menu.NONE, "标记为重要");
menu.add(0, 3, Menu.NONE, "重命名");
menu.add(0, 4, Menu.NONE, "删除");
}

3)响应上下文菜单项
与响应options menu类似,唯一的不同是可以通过menu info获得额外的信息。

 @Override
publicboolean onContextItemSelected(MenuItem item) {
// 得到当前被选中的item信息
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
Log.v(TAG, "context item seleted ID="+ menuInfo.id);

switch(item.getItemId()) {
case1:
// do something
break;
case2:
// do something
break;
case3:
// do something
break;
case4:
// do something
break;
default:
returnsuper.onContextItemSelected(item);
}
returntrue;
}

运行程序,多次唤出上下文菜单,注意LogCat的输出,onCreateContextMenu每一次都被调用了。