Use both onClickListener and onLongClickListener in listview Android 1.6
Asked Answered
T

4

40

I am using both onClickListener and onLongClickListener for a TextView in a ListView. I see that in Android 1.6, the long click listener is fired along with the on click listener meaning both are fired when I long click. But this is not the case in the future versions. Is there any fix for this?

@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();
      }
  });
}
Thomasinathomasine answered 7/1, 2011 at 0:25 Comment(8)
What "fix"? The fact that it fired both in 1.6 is certainly a bug.Giefer
By fix, I mean, is there any way to prevent this problem.Thomasinathomasine
Are they called consistently in the same order? Which?Microbicide
Are the callbacks called at exactly the same time when you make a longclick?Respondent
First the longClick event is performed followed by the clickevent.Thomasinathomasine
When I place my finger for 2 sec, onlongclick listener is called and when I lift my finger, click listener is called.Thomasinathomasine
strangeoptics.blogspot.com/2011/09/…Pemmican
You should check the android documentation, it's quite useful: developer.android.com/reference/android/view/…Ragg
B
127

Did you return boolean true at the end of OnLongClickListener to indicate you don't want further processing?

Balladeer answered 10/4, 2011 at 21:52 Comment(0)
M
14

I think you you should use OnItemLongClickListener() instead of OnLongClickListener().

See developers website for further response

Mucilaginous answered 10/3, 2012 at 9:53 Comment(0)
G
13
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;
    }
});
Geffner answered 19/7, 2012 at 5:43 Comment(0)
K
3
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; }});
Kurtkurth answered 15/4, 2016 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.