Skip to content

Instantly share code, notes, and snippets.

@tir38
Last active December 25, 2015 06:13
Show Gist options
  • Save tir38/3d03f5b94c30595fdeeb to your computer and use it in GitHub Desktop.
Save tir38/3d03f5b94c30595fdeeb to your computer and use it in GitHub Desktop.
Extends Daniel Olshansky's DevByte example "ListView Expanding Cells Animation" to allow for custom collapsable height http://www.youtube.com/watch?v=mwE61B56pVQ http://developer.android.com/shareables/devbytes/ListViewExpandingCells.zip

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

  1. 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:

Imgur

Smooth:

Imgur

@amalChandran
Copy link

The flicker while colpase animation still persists.

@depodefi
Copy link

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);

@mepunit
Copy link

mepunit commented Dec 25, 2015

Hi..

In the same example, I wish to expand only 1 list item at a time. So, if any other item is expanded, it should be collapsed. Please tell me how to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment