I want to do some works while the user hold his hand on a view and stop it when he release it. My code is below:
viewHolder.plus.setOnTouchListener(new View.OnTouchListener() {
private Handler mHandler;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(mHandler==null)
mHandler = new Handler();
mHandler.postDelayed(mAction, 500);
return true;
case MotionEvent.ACTION_UP:
Log.v("this","cancel");
if (mHandler != null) {
mHandler.removeCallbacks(mAction);
mHandler = null;
Log.v("this", "c cancel");
}
break;
}
return false;
}
Runnable mAction = new Runnable() {
@Override public void run() {
ChangeTedad(item.getid(), "plus");
Log.v("this","Cc");
mHandler.postDelayed(this, 500);
}
};
});
the problem is , ACTION_UP is never called and the runnable stays run forever .
How to stop the runnable when the user un touch it ?