在listview Android 1.6中同时使用onClickListener和onLongClickListener

时间:2011-01-07 00:25:37

标签: android listview onlongclicklistener

我在 ListView 中使用onClickListeneronLongClickListener来获取 TextView 。我看到在Android 1.6中,长时间点击监听器与点击监听器一起被激活,这意味着当我长按时,两者都被触发。但在未来的版本中并非如此。有没有解决这个问题?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

  if (convertView == null) {
    LayoutInflater inflater = getLayoutInflater();
    row = inflater.inflate(R.layout.row, parent, false);
  }

  TextView tv = (TextView) row.findViewById(R.id.tv);

  tv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        showMessage();
      }
  });

  tv.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
        showLongMessage();
      }
  });
}

4 个答案:

答案 0 :(得分:105)

您是否在OnLongClickListener的末尾返回布尔true,表示您不想进一步处理?

答案 1 :(得分:13)

我认为你应该使用OnItemLongClickListener()而不是OnLongClickListener()。

有关进一步的回复,请参阅developers website

答案 2 :(得分:7)

TextView t1 = (TextView) findViewById(R.id.textView1);
t1.isClickable();

t1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show();
    }
});

t1.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
});

答案 3 :(得分:1)

itemToClick.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View v)      { 
      //do your logic on click 
     });
itemToClick.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
      // do your logic for long click and remember to return it 
        return true; }});