Android ExpandableListView using animation

后端 未结 5 2004
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 05:39

I\'m using



        
相关标签:
5条回答
  • 2020-12-05 05:51

    Found this snnipet not remebering where here in Stack Overflow. Have two basic static methods: expand(View v) and collapse(View v). You only have to pass the view you want to hide show.

    Note: I Don't recomend pass a view having wrap_content as height. May not work fine.

     public class expand {
    
        public static void expand(View view) {
            view.setVisibility(View.VISIBLE);
    
            final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(widthSpec, heightSpec);
    
            ValueAnimator mAnimator = slideAnimator(view, 0, view.getMeasuredHeight());
            mAnimator.start();
        }
    
    
        public static void collapse(final View view) {
            int finalHeight = view.getHeight();
    
            ValueAnimator mAnimator = slideAnimator(view, finalHeight, 0);
    
            mAnimator.addListener(new Animator.AnimatorListener() {
    
                @Override
                public void onAnimationEnd(Animator animator) {
                    view.setVisibility(View.GONE);
                }
    
    
                @Override
                public void onAnimationStart(Animator animation) {
    
                }
    
    
                @Override
                public void onAnimationCancel(Animator animation) {
    
                }
    
    
                @Override
                public void onAnimationRepeat(Animator animation) {
    
                }
            });
            mAnimator.start();
        }
    
    
        private static ValueAnimator slideAnimator(final View v, int start, int end) {
    
            ValueAnimator animator = ValueAnimator.ofInt(start, end);
    
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
    
                    int value = (Integer) valueAnimator.getAnimatedValue();
                    ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                    layoutParams.height = value;
                    v.setLayoutParams(layoutParams);
                }
            });
            return animator;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:01

    @idunnololz solution is working great, but I experienced weird behavior with my custom layout for group. The expand operation was not executed properly, the collapse however worked perfect. I imported his test project and it worked just fine, so I realized the problem is with my custom layout. However when I was not able to locate the problem after some investigation, I decided to uncomment these lines of code in his AnimatedExpandListView:

           if (lastGroup && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                return expandGroup(groupPos, true);
            }
    

    which caused the problem (my app is aimed for Android 4.0+).

    0 讨论(0)
  • 2020-12-05 06:03
    animateLayoutChanges adds auto-animation
    <ExpandableListView
            android:animateLayoutChanges="true"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    
    0 讨论(0)
  • 2020-12-05 06:07

    @idunnololz solution works great. however i would like to add some code to collapse previously expanded group.

    private int previousGroup=-1;
    
        listView.setOnGroupClickListener(new OnGroupClickListener() {
    
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                // We call collapseGroupWithAnimation(int) and
                // expandGroupWithAnimation(int) to animate group 
                // expansion/collapse.
                if (listView.isGroupExpanded(groupPosition)) {
                    listView.collapseGroupWithAnimation(groupPosition);
                    previousGroup=-1;
                } else {
                    listView.expandGroupWithAnimation(groupPosition);
                    if(previousGroup!=-1){
                        listView.collapseGroupWithAnimation(previousGroup); 
                    }
                    previousGroup=groupPosition;
                }
    
                return true;
            }
    
        });
    
    0 讨论(0)
  • 2020-12-05 06:09

    Final Update

    It's been quite a while since I wrote this answer. Since then a lot has changed. The biggest change is with the introduction of RecyclerView that makes animating a list or grid easy. I highly recommend switching over to RecyclerViews if you can. For those who can't I will see what I can do regarding fixing the bugs for my library.


    Original answer

    I actually do not like the popular implementation of an animated ExpandableListView that simply uses a ListView with an expand animation because in my use case, each of my groups had a lot of children, therefore it was not feasible to use a normal ListView as the child views will not be recycled and the memory usage will be huge with poor performance. Instead, I went with a much more difficult but more scalable and flexible approach.

    I extended the ExpandableListView class and overrode the onCollapse and onExpand functions, I also created a subclass of a BaseExpandableListAdapter called AnimatedExpandableListAdapter. Inside the adapter, I overrode the getChildView function and made the function final so that the function cannot be overrode again. Instead I provided another function called getRealChildView for subclasses to override to provide a real child view. I then added an animation flag to the class and made getChildView return a dummy view if the animation flag was set and the real view if the flag was not set. Now with the stage set I do the following for onExpand:

    1. Set the animation flag in the adapter and tell the adapter which group is expanding.
    2. Call notifyDataSetChanged() (forces the adapter to call getChildView() for all views on screen).
    3. The adapter (in animation mode) will then create a dummy view for the expanding group that has initial height 0. The adapter will then get the real child views and pass these views to the dummy view.
    4. The dummy view will then start to draw the real child views within it's own onDraw() function.
    5. The adapter will kick off an animation loop that will expand the dummy view until it is of the right size. It will also set an animation listener so that it can clear the animation flag once the animation completes and will call notifyDataSetChanged() as well.

    Finally with all of this done, I was able to not only get the desired animation effect but also the desired performance as this method will work with group with over 100 children.

    For the collapsing animation, a little more work needs to be done to get this all setup and running. In particular, when you override onCollapse, you do not want to call the parent's function as it will collapse the group immediately leaving you no chance to play an animation. Instead you want to call super.onCollapse at the end of the collapse animation.

    UPDATE:

    I spent some time this weekend to rewrite my implementation of this AnimatedExpandableListView and I'm releasing the source with an example usage here: https://github.com/idunnololz/AnimatedExpandableListView/

    0 讨论(0)
提交回复
热议问题