0

我有一个带有 Custon 适配器的列表视图。我为 longclick 实现了一个侦听器来处理上下文菜单。问题是长按不起作用。上下文菜单没有出现。

以下是代码:

来自列表视图的监听器

    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);
}

来自列表视图的 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" >
4

2 回答 2

2

您必须在活动 onCreate 方法上注册您的 listView 以获取上下文菜单:

registerForContextMenu(lv);  

view.setLongClickable(true);适配器上 不需要这条线

而且,在这种特殊情况下,setOnItemLongClickListener应该return false

于 2014-01-09T19:33:20.087 回答
0

如果 setOnItemLongClickListener 被使用,它应该返回 true。见这里

   lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {              
            seguro = (SeguroSelecaoModel)adapter.getItem(position);
            return true;
        }
    });
于 2014-01-09T18:37:54.147 回答