android 仿IOS滑动删除动画

Demo是基于SwipeMenuListView的基础上实现的。先看效果图:
这里写图片描述

实现思路:

我觉得其实无论些什么模块,必须先有一个清晰的实现思路,这会事倍功半。
建议先了解SwipeMenuListView和属性动画的使用。
这里关键是使用属性动画实现删除后ITEM的向上或者向下移动。触发删除后,把上面或者下面可见的ITEM加上向上移动的动画,然后动画结束后刷新listview即可。效果有点小瑕疵,因为隐藏的View没法获取。所以最下面向上移的时候会空出一个ITEM,因为时间间隔设的有点短,瑕疵效果不太明显。哪位大神可以给点意见这个怎么解决?
核心代码:

swipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // open

                        break;
                    case 1:
                        // delete
                        startDeleteAnimator(position);
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
   private void startDeleteAnimator(final int position) {
        View view = swipeMenuListView.getChildAt(position - swipeMenuListView.getFirstVisiblePosition());
        ObjectAnimator.ofFloat(view, "alpha", 1, 0).setDuration(20).start();
        if (swipeMenuListView.getFirstVisiblePosition() != 0 && swipeMenuListView.getLastVisiblePosition() == swipeMenuListView.getCount() - 1) {
            //向下移
            for (int i = position - 1 - swipeMenuListView.getFirstVisiblePosition(); i >= 0; i--) {
                View itemView = swipeMenuListView.getChildAt(i);
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(itemView, "translationY", view.getHeight()).setDuration(200);
                objectAnimator.start();
                if (i == 0) {
                    objectAnimator.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            data.remove(position);
                            adapter.notifyDataSetChanged();
                            // 动画结束后,恢复ListView所有子View的属性
                            for (int i = 0; i < swipeMenuListView.getChildCount(); ++i) {
                                View v = swipeMenuListView.getChildAt(i);
                                v.setAlpha(1f);
                                v.setTranslationY(0);
                            }
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                }
            }
        } else {
            //向上移
            if (position == swipeMenuListView.getCount() - 1) {
                data.remove(position);
                adapter.notifyDataSetChanged();
            }
            for (int i = position + 1 - swipeMenuListView.getFirstVisiblePosition(); i < swipeMenuListView.getChildCount(); i++) {
                View itemView = swipeMenuListView.getChildAt(i);
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(itemView, "translationY", -view.getHeight()).setDuration(200);
                objectAnimator.start();
                if (i == swipeMenuListView.getChildCount() - 1) {
                    objectAnimator.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            data.remove(position);
                            adapter.notifyDataSetChanged();

                            // 动画结束后,恢复ListView所有子View的属性
                            for (int i = 0; i < swipeMenuListView.getChildCount(); ++i) {
                                View v = swipeMenuListView.getChildAt(i);
                                v.setAlpha(1f);
                                v.setTranslationY(0);
                            }
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                }
            }
    }
    }

越来越懒了。Demo下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值