Animate the removal of a ListView item

后端 未结 4 1855
生来不讨喜
生来不讨喜 2021-01-30 05:33

I\'m attempting to animate the removal of a ListView item using this:

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Overr         


        
4条回答
  •  滥情空心
    2021-01-30 06:03

    As Chet Haase points out in DevBytes, https://www.youtube.com/watch?v=8MIfSxgsHIs, all of this will work but don't forget to add transientState flags.

    From his code:

    // Here's where the problem starts - this animation will animate a View object.
                        // But that View may get recycled if it is animated out of the container,
                        // and the animation will continue to fade a view that now contains unrelated
                        // content.
                        ObjectAnimator anim = ObjectAnimator.ofFloat(view, View.ALPHA, 0);
                        anim.setDuration(1000);
                        if (setTransientStateCB.isChecked()) {
                            // Here's the correct way to do this: if you tell a view that it has
                            // transientState, then ListView ill avoid recycling it until the
                            // transientState flag is reset.
                            // A different approach is to use ViewPropertyAnimator, which sets the
                            // transientState flag internally.
                            view.setHasTransientState(true);
                        }
                        anim.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                cheeseList.remove(item);
                                adapter.notifyDataSetChanged();
                                view.setAlpha(1);
                                if (setTransientStateCB.isChecked()) {
                                    view.setHasTransientState(false);
                                }
                            }
                        });
                        anim.start();
    

提交回复
热议问题