androidlistviewtransitiondrawable

android listview: cell transition drawable with a longclicklistener


I have a listview with a OnItemClickListener and a OnItemLongClickListener.

In the getView method of my listadapter, I set a view with a TransitionDrawable as background, next I apply a OnTouchListener to the view so that when the user presses the view and holds it, he will see the transition happen.

The issue is that the OnTouchListener must return true, otherwise only the ACTION_DOWN motionevent will be captured. But by returning true, my OnItemClickListener and OnItemLongClickListener aren't fired.

The question is how can I both show the TransitionDrawable's animation AND fire the click events?

getView(..) of the arrayadapter:

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

    ...

    convertView.setOnTouchListener(touchListener);
    return convertView;
}

OnTouchLIstener:

OnTouchListener touchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {         
        TransitionDrawable transition = (TransitionDrawable)v.getBackground();

        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            transition.startTransition(ViewConfiguration.getLongPressTimeout());
            return true;
        }
        else {
            transition.resetTransition();
        }

        return false;
    }
};

Solution

  • I ended up creating an interface for click and longclick myself and handling them in the onTouchListener:

    private class TransitionView implements OnTouchListener {
        private Object object;
        TransitionDrawable transition;
        CountDownTimer timer;
        boolean running;
        View v;
    
        public TransitionView(Object object) {
            this.object = object;
    
            int time = ViewConfiguration.getLongPressTimeout();
            timer = new CountDownTimer(time, time) {
    
                @Override
                public void onTick(long millisUntilFinished) { }
    
                @Override
                public void onFinish() {
                    running = false;
                    transition.resetTransition();
                    longClickListener.onItemLongClick(TransitionView.this.object, v);
                }
            };
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            this.v = v;
    
            transition = (TransitionDrawable)v.getBackground();
    
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                transition.startTransition(ViewConfiguration.getLongPressTimeout());
                timer.start();
                running = true;
                return true;
            }
            else if(event.getAction() == MotionEvent.ACTION_UP) {
                if(running) {
                    timer.cancel();
                    running = false;
                    itemClickListener.onListItemClick(object, v);
                }
                transition.resetTransition();
            }
            else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
                transition.resetTransition();
            }
    
            return false;
        }
    }