Ruuhkis
Ruuhkis

Reputation: 1934

Animating Android ListView view on touch

I'd like to animate item in ListView when user touches the item and do the animation backwards when the touch have ended.

I've tried doing it by overriding onTouchEvent of the list item but if I return true when I handle the event I don't receive OnItemClickListener calls anymore because I've consumed the touch event, and if I return false I don't receive callback when user stops touching the view.

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            //handle onclick
        }
    });

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.v(TAG, event.getActionMasked() + "");
    if(event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        Animation animation = createColorAnimation(false);
        animation.setDuration(500);
        startAnimation(animation);
        return true;
    } else if(event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
        Animation animation = createColorAnimation(true);
        animation.setDuration(500);
        startAnimation(animation);
        return true;
    }
    return super.onTouchEvent(event);
}

I want to receive ACTION_UP and ACTION_CANCEL events onTouchEvent and calls OnItemClickListener, how do I achieve this?

Upvotes: 1

Views: 1453

Answers (1)

pskink
pskink

Reputation: 24750

in your list adapter in method getView() before returning View v call v.setBackgroundDrawable with a custom Drawable. this Drawable has to be stateful (isStatefull shound return true), overwrite onStateChanged and log StateSet.dump(stateSet), the rest is up to you what you do with each state

Upvotes: 2

Related Questions