使用ViewPager内放Fragment,Fragment中有一个ListView,在对ListView的item进行注册上下文菜单时,发现无论怎样长按,选择的都是第一个Fragment。
stackoverflow上的回复是在onContextItemSelected()中加上一个 getUserVisibleHint()方法进行判断就好了。不知道其中的原委,不过赶时间先把坑挖在这里,闲下来了回来消化消化
public boolean onContextItemSelected(android.view.MenuItem item) {
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
if (getUserVisibleHint()) {
switch (item.getItemId()) {
case 0:
list.remove(info.position);
Toast.makeText(mContext, type + info.position + "deleted",
Toast.LENGTH_SHORT).show();
break;
case 1:
list.remove(info.position);
Toast.makeText(mContext, type + info.position + "blocked",
Toast.LENGTH_SHORT).show();
break;
}
adapter.notifyDataSetChanged();
return true;
}
return super.onContextItemSelected(item);
}