In Daniel's example, all rows contain the same view, so their collapsed heights are all the same. However if you have different size collapsed rows, the animation will be jumpy. To fix this
- pull the cell height out of the constructor for ExpandableListItem:
public class ExpandableListItem implements OnSizeChangedListener { private String mTitle; private String mText; private boolean mIsExpanded; private int mImgResource; private int mCollapsedHeight; private int mExpandedHeight;public ExpandableListItem(String title, int imgResource, int collapsedHeight, String text) {public ExpandableListItem(String title, int imgResource, String text) { mTitle = title; mImgResource = imgResource;mCollapsedHeight = collapsedHeight;mIsExpanded = false; mText = text; mExpandedHeight = -1; }
2. inside ExpandingListView, set the collapsed height as soon as you are about to expand the item:
private void expandView(final View view) { final ExpandableListItem viewObject = (ExpandableListItem)getItemAtPosition(getPositionForView (view)); // set its collapsable height to its current height, before expanding viewObject.setCollapsedHeight(view.getHeight()); /* Store the original top and bottom bounds of all the cells.*/ final int oldTop = view.getTop(); final int oldBottom = view.getBottom(); ... }
Now all of your cells will collapse back to their original height and the animation will be smooth the whole way. As a side note, you can turn up the Animator Duration Scale so that you can see the transitions happen in slow-motion: http://www.cnet.com/how-to/speed-up-your-android-by-adjusting-animation-settings/
Jumpy:
Smooth:
I think you are missing change in CustomArrayAdapter in getView() method instead of:
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams
(AbsListView.LayoutParams.MATCH_PARENT, object.getCollapsedHeight());
it should be:
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams
(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT);