How to animate text change in TextView?

2020-07-03 04:11发布

问题:

Trying to do the following:

animTimeChange = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); 
itemTime.startAnimation(animTimeChange);
itemTime.setText("new text");

but the animation happens thru blank screen (i.e. original text is cleared, then new text appears with animation). How to avoid that blank screen?

(my TextView is part of ListView row, I've tried to use TextSwitcher - it doesn't work properly; for ViewFlipper - I am not sure where add Views there, since this is part of the ListView)

回答1:

TextSwitcher is exactly what you should be using for this. Check out the API Demo for TextSwitcher.

The way you should implement this is in your ListAdapter, provide TextSwitcher views to the ListView instead of TextViews. Then you can just call TextSwitcher.setText() on the list item you want to change.

Note that you should imediately get rid of your reference to the list item to avoid REALLY messing up listview.