我有一个带有Custon适配器的列表视图。我为longclick实现了一个监听器来处理上下文菜单。问题是长按不起作用。上下文没有出现。
以下是代码:
来自listview的监听器
registerForContextMenu(lv);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
seguro = (SeguroSelecaoModel)adapter.getItem(position);
return false;
}
});
来自自定义适配器的代码
public View getView(final int index, View view, ViewGroup parent) {
if(view == null){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.linha_selecionar_seguro, parent, false);
}
final SeguroSelecaoModel seguro = (SeguroSelecaoModel)getItem(index);
view.setLongClickable(true);
TextView tvNome = (TextView) view.findViewById(R.id.tvNomeSeguro);
tvNome.setText(seguro.getNomeSeguradora());
TextView tvData = (TextView) view.findViewById(R.id.tvDataValidadeSelecionarSeguro);
tvData.setText(seguro.getDataValidade());
ImageView ivCell = (ImageView) view.findViewById(R.id.ivCellSelecionarSeguro);
Button btEdicao = (Button) view.findViewById(R.id.btEditarSeguro);
TextView labelSeguro = (TextView) view.findViewById(R.id.labelSeguroSelecionarSeguro);
TextView labelValidade = (TextView) view.findViewById(R.id.labelValidadeSelecionarSeguro);
if(seguro.isAtivo() == true){
ivCell.setBackgroundResource(R.drawable.cellselectedbackground);
tvNome.setTextColor(view.getResources().getColor(R.color.amarelo));
tvData.setTextColor(view.getResources().getColor(R.color.amarelo));
btEdicao.setBackgroundResource(R.drawable.goldbuttonarrow);
labelSeguro.setTextColor(view.getResources().getColor(R.color.vermelho));
labelValidade.setTextColor(view.getResources().getColor(R.color.vermelho));
} else{
ivCell.setBackgroundResource(R.drawable.cellbg);
tvNome.setTextColor(Color.WHITE);
tvData.setTextColor(Color.WHITE);
btEdicao.setBackgroundResource(R.drawable.bt_seta);
labelSeguro.setTextColor(view.getResources().getColor(R.color.cinza));
labelValidade.setTextColor(view.getResources().getColor(R.color.cinza));
}
btEdicao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, DadosSeguro1Activity.class);
intent.putExtra("idSeguro", seguro.getId());
context.startActivity(intent);
//((Activity) context).finish();
}
});
return view;
}
上下文菜单中的代码
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(seguro.getNomeSeguradora());
menu.add("Selecionar Seguro");
menu.add("Excluir Seguro");
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
sDAO.open();
if(item.getTitle().equals("Excluir Seguro")){
sDAO.selecionaSeguro(seguro.getId());
adapter.clearList();
buildList();
sDAO.close();
} else if(item.getTitle().equals("Selecionar Seguro")){
sDAO.removeSeguro(seguro.getId());
}
return super.onMenuItemSelected(featureId, item);
}
来自listview的xml
<ListView
android:id="@+id/lvSeguroPrincipal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/imgFaixaSeguro2"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imgFaixaSeguro"
android:divider="@android:color/transparent"
android:longClickable="true" >
答案 0 :(得分:2)
您必须在活动onCreate方法上注册listView以获取上下文菜单:
registerForContextMenu(lv);
您在适配器
上不需要此行view.setLongClickable(true);
而且,在这种特殊情况下,setOnItemLongClickListener
应return false
答案 1 :(得分:0)
如果使用了setOnItemLongClickListener,则返回true。请参阅here。
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
seguro = (SeguroSelecaoModel)adapter.getItem(position);
return true;
}
});