Animating Android ListView view on touch

ぃ、小莉子 提交于 2019-12-24 12:28:25

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/16999443/animating-android-listview-view-on-touch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!