Coderwall created a layer list XML drawable in order to achieve this. A big thanks to him! This is how the XML file mydrawable.xml looks like:

Make sure you place this in the drawable folder.

To use this, simple set it as the value of the android:background attribute of any layout in your XML.

Most likely you encountered this exception

ClassCastException: LinearLayout$LayoutParams Cannot Be Cast to AbsListView$LayoutParams

because you either added a header and/or footer in the ListView component and when you removed them, you found out there was no error generated.

Explanations aside, to fix this problem just set the LayoutParams of your header or footer like this:

That should solve it.

Most expand and collapse animation code I saw in Android related forums never worked. I think that they probably are limited to just widgets. When the View object itself e.g. LinearLayout has children within it, then those animation XMLs or code to animate them during runtime do not work.

The closest code that worked is from Tom Esterez. He describes it as a dirty hack but I think that it is the only code that is shared publicly that works. However, the code he shared is limited to expanding the ViewGroup.

I modified his existing code to also cater a collapse version. The method returns Animation object.

Feel free to change the animation speed according to your preference. The lower the value, the faster the animation speed will be.

Expand the code area section to view the method in full.

Related Posts Plugin for WordPress, Blogger...