Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Animating changes to GridView content

Tags:

android

I have a GridView of layouts which can be dynamically added or removed to the grid.

In iOS, the native behavior for GridView items is for the other items in the grid to slide into the place of the removed item, or slide to make room when a new item is added.

However on Android the behavior is just for the changes to instantly pop in on the screen. I tried adding custom animations to each getView() call using some tricks in the GridView's adapter, but that ended up causing some problems seen here:

Android GridView loading the 0 indexed item in a later index's slot when data set changes

I tried having the individual views control the animation instead of the getView() method of the adapter, but the end result was identical.

I also tried using a GridLayoutAnimationController, like this:

Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.grid_item_fadein); GridLayoutAnimationController controller = new GridLayoutAnimationController(animation, .2f, .2f); mGrid.setLayoutAnimation(controller); 

This works on the initial load of all GridView content, but any changes to that content just instantly pop in like it does by default.

I feel like this is a pretty simple feature, and there should be an easier way to do it. The best I've seen from searching online is a library which animates GridView items that swap places in the grid, but that isn't what I'm looking for (in that same library, when you remove an item from the Grid via deletion the same instant effect that I'm trying to avoid occurs).

A common answer on SO I've also seen is "Look at the APIDemos, there are tons of examples." But as far as I can see, there are actually no examples for animating GridView content changes.

Any help is appreciated, thanks.

like image 919
JMRboosties Avatar asked Apr 08 '13 23:04

JMRboosties


People also ask

What is transition animation in Android?

Android Transition Framework can be used for three main things: Animate activity layout content when transitioning from one activity to another. Animate shared elements (Hero views) in transitions between activities. Animate view changes within same activity.


Video Answer


1 Answers

Chet Haase came out with a nice video on how to handle ListView animations properly. The basic concept can be applied to GridViews. You can find the video here.

If you're developing for API 1.0+, you won't be able to use the referenced animation features that the video mentions (such as ViewPropertyAnimator) unless you use NineOldAndroids.

like image 152
Alex Fu Avatar answered Oct 01 '22 06:10

Alex Fu