onItemClick有效,但onItemLongClick没有,为什么?

时间:2014-09-06 06:53:46

标签: android onclicklistener

我喜欢在长按一下时分享项目,我使用onItemClick转到另一个意图并使用onItemLongClick进行分享但onItemLongClick无效请帮助我

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {      
    ViewHolder ocHolder =  (ViewHolder)view.getTag();
    TextView ocTextID =(TextView)ocHolder.txtID;
    TextView ocTextDsc =(TextView)ocHolder.txtDsc;

    Intent intents = new Intent(Intent.ACTION_SEND);
    intents.setType("text/plain");
    String my_string = ocTextTitle.getText().toString();
    intents.putExtra(Intent.EXTRA_TEXT, my_string);
    startActivity(Intent.createChooser(intents, "Share this text via"));

    return false;  
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    ViewHolder ocHolder =  (ViewHolder)view.getTag();
    TextView ocTextID =(TextView)ocHolder.txtID;
    TextView ocTextTitle =(TextView)ocHolder.txtTitle;

    Intent i = new Intent(hozeactivity.this, ChildList.class);
    i.putExtra("pId", ocTextID.getText().toString() );
    i.putExtra("uName", ocUser );
    i.putExtra("pass", ocPass);
    i.putExtra("isOnline", isOnline);

    startActivity(i);
}

1 个答案:

答案 0 :(得分:0)

return false;更改为return true;,告诉您正在处理该事件的视图。修改您的代码如下:

  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {      
    ViewHolder ocHolder =  (ViewHolder)view.getTag();
    TextView ocTextID =(TextView)ocHolder.txtID;
    TextView ocTextDsc =(TextView)ocHolder.txtDsc;

    Intent intents = new Intent(Intent.ACTION_SEND);
    intents.setType("text/plain");
    String my_string = ocTextTitle.getText().toString();
    intents.putExtra(Intent.EXTRA_TEXT, my_string);
    startActivity(Intent.createChooser(intents, "Share this text via"));

    return true;  

}

还要确保ListView注册setOnItemLongClickListener():

myListView.setOnItemLongClickListener(listener);