1

I have a custom adapter where I override the isEnabled()method.

I have noticed that when the method gets called, it's called on, touch down and on, touch up. This is kind of irritating if you want to perform some action.

Lets say you show a Toast effect. The effect will flash up twice.

Example:

@Override
public boolean isEnabled(int position) {

    Toast.makeText(mContext, "Hello", Toast.LENGTH_SHORT).show(); // this will flash up twice
    return true;
}

Is there a way of disabling the touch down for the isEnabled() method?

HGPB
  • 4,346
  • 8
  • 50
  • 86
  • 2
    I think `isEnabled` is not supposed to be used as a callback to trigger this kind of action, but it's intended to be overriden to provide some information. In other words you should not have any insight or expectation on how and when it gets called – sdabet Nov 23 '12 at 12:39
  • 1
    Yes `isEnabled` is definitely not meant to perform actions. You should fix your approach, there is no guarantee that `isEnabled` is called on any touch. So the next Android version could break your code. Furthermore, the whole `Adapter` is not the best place to perform actions. Take the `ListView` (or whatever uses the Adapter) and use it's `setOnItemClickListener` method to perform actions on click. – zapl Nov 23 '12 at 12:55
  • I agree with that. My situation is kind of unique in that I require the user to be informed when the list item is clicked and disabled. I can manage this quite nicely by extending the adapter and overriding isEnabled(). – HGPB Nov 23 '12 at 12:57
  • @zapl I'll do that for the toast now you mention it. Thanks. – HGPB Nov 23 '12 at 12:58

1 Answers1

1

use an generic variable in your adapter,

Boolean isFirstTime=true;

@Override
public boolean isEnabled(int position) {

    if(isFirstTime){

        Toast.makeText(mContext, "Hello", Toast.LENGTH_SHORT).show(); // this will flash up twice
        isFirstTime = false;
    }


    return true;
}
Talha
  • 12,673
  • 5
  • 49
  • 68
  • My solution: `if(mToastCounter == 1) { Toast.makeText(mContext, Message, Toast.LENGTH_LONG).show(); mToastCounter = 0; } mToastCounter++;` – HGPB Nov 23 '12 at 12:46
  • it is true, but mToastCounter will always grow by ++ :) I think you should use boolean as i do :) – Talha Nov 23 '12 at 12:48
  • No, it gets reset in if statement... It ignores the touch down and captures the touch up. – HGPB Nov 23 '12 at 12:49
  • It doesnt ignore touchdown. It only skips toast message :) – Talha Nov 23 '12 at 12:52
  • Technically speaking yes, it doesn't ignore it, just skips it. – HGPB Nov 23 '12 at 12:53