Recycler View动画,旧的ViewHolder仍然可见

时间:2015-10-20 11:45:55

标签: android android-recyclerview recycler-adapter recyclerview-layout

我正在尝试为现有的回收站视图创建一个简单的动画幻灯片。

让我们说回收商拥有50件物品,在某些时候数据集已经改变,现在只包含40件物品,物品已被更换,之前的50件物品不再相关。

因此,在修改数据结构并为新项目设置动画后,将调用notifyDatasetChanged()。

问题是您仍然可以看到新项目下面的前40个项目,在每个单元格的相同空间中,您可以看到以前的数据和新数据。

下面是ItemAnimator子类的代码,如果我添加一个删除动画来改变删除单元格的不透明度,它将是不可见的,但项目装饰(列表行)被not删除,我宁愿删除这些物品完全不会让它变得不可见。

public class RVSlideAnimation extends RecyclerView.ItemAnimator {

    List<RecyclerView.ViewHolder> mViewHolders = new ArrayList<RecyclerView.ViewHolder>();

    @Override
    public void runPendingAnimations() {
        if (!mViewHolders.isEmpty()) {
            int animationDuration = 250;
            AnimatorSet animator;
            View target;
            for (final RecyclerView.ViewHolder viewHolder : mViewHolders) {
                target = viewHolder.itemView;
                target.setPivotX(target.getMeasuredWidth() / 2);
                target.setPivotY(target.getMeasuredHeight() / 2);

                animator = new AnimatorSet();

                animator.playTogether(
                        ObjectAnimator.ofFloat(target, "translationX", target.getMeasuredWidth(), 0.0f),
                        ObjectAnimator.ofFloat(target, "alpha", target.getAlpha(), 1.0f)
                );

                animator.setTarget(target);
                animator.setDuration(animationDuration);
                animator.setInterpolator(new AccelerateDecelerateInterpolator());
                animator.setStartDelay((animationDuration * viewHolder.getAdapterPosition()) / 10);
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mViewHolders.remove(viewHolder);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animator.start();
            }
        }
    }

    @Override
    public boolean animateRemove(RecyclerView.ViewHolder viewHolder) {
        //viewHolder.itemView.animate().alpha(0).setDuration(100);
        return false;
    }

    @Override
    public boolean animateAdd(RecyclerView.ViewHolder viewHolder) {
        viewHolder.itemView.setAlpha(0.0f);
        return mViewHolders.add(viewHolder);
    }

    @Override
    public boolean animateMove(RecyclerView.ViewHolder viewHolder, int i, int i2, int i3, int i4) {
        return false;
    }

    @Override
    public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop) {
        return false;
    }

    @Override
    public void endAnimation(RecyclerView.ViewHolder viewHolder) {
    }

    @Override
    public void endAnimations() {
    }

    @Override
    public boolean isRunning() {
        return !mViewHolders.isEmpty();
    }

} 

1 个答案:

答案 0 :(得分:0)

recyclerView.destroyDrawingCache();在notifyDatasetChaged()为我解决了这个问题之前。