How can I make a cell in a ListView in Android expand and contract vertically when it's touched?

前端 未结 6 1099
别那么骄傲
别那么骄傲 2020-12-01 08:20

I have a cell in a ListView that has a bunch of text in it. I show the first two rows of text and then end it with a \"...\" if it goes beyond. I want a user to be able to t

6条回答
  •  孤街浪徒
    2020-12-01 08:51

    Here is example from Udinic. It had listview item expand with animation and require API level only 4+

    ExpandAnimationExample

    in onItemClick event use ExpandAnimation

    /**
    * This animation class is animating the expanding and reducing the size of a view.
    * The animation toggles between the Expand and Reduce, depending on the current state of the view
    * @author Udinic
    *
    */
    public class ExpandAnimation extends Animation {
        private View mAnimatedView;
        private LayoutParams mViewLayoutParams;
        private int mMarginStart, mMarginEnd;
        private boolean mIsVisibleAfter = false;
        private boolean mWasEndedAlready = false;
    
        /**
    * Initialize the animation
    * @param view The layout we want to animate
    * @param duration The duration of the animation, in ms
    */
        public ExpandAnimation(View view, int duration) {
    
            setDuration(duration);
            mAnimatedView = view;
            mViewLayoutParams = (LayoutParams) view.getLayoutParams();
    
            // decide to show or hide the view
            mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);
    
            mMarginStart = mViewLayoutParams.bottomMargin;
            mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);
    
            view.setVisibility(View.VISIBLE);
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
    
            if (interpolatedTime < 1.0f) {
    
                // Calculating the new bottom margin, and setting it
                mViewLayoutParams.bottomMargin = mMarginStart
                        + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
    
                // Invalidating the layout, making us seeing the changes we made
                mAnimatedView.requestLayout();
    
            // Making sure we didn't run the ending before (it happens!)
            } else if (!mWasEndedAlready) {
                mViewLayoutParams.bottomMargin = mMarginEnd;
                mAnimatedView.requestLayout();
    
                if (mIsVisibleAfter) {
                    mAnimatedView.setVisibility(View.GONE);
                }
                mWasEndedAlready = true;
            }
        }
    }
    

    Detail usage is in project.

提交回复
热议问题