Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView animate single item

I got a ListView with items in it. When the user clicks an item it's height should scale to zero and all items below should scroll up. My code below doesnt work. With my code the item which was clicked scales right, but the items below dont scroll up, they stay at the same position. I've also tried it with an LinearLayout but there is the same problem.

There is an app which does this right. It's called Tasks.

This little image should explain the problem

My current implementation looks like this:

@Override
public void onItemClick(AdapterView<?> arg0, View v, final int index,
        long id) {
    Animation anim = AnimationUtils.loadAnimation(getActivity(),
            R.anim.scaleup);
    v.startAnimation(anim);
}

<set android:shareInterpolator="false" >
    <scale
        android:duration="700"
        android:fillAfter="false"
        android:fillBefore="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="0.0" />
</set>

like image 762
Basic Coder Avatar asked Aug 17 '12 16:08

Basic Coder


2 Answers

Here's a class I made (modified from source I found here) that may give you the functionality you want.

public class FadeUpAnimation extends Animation {

int mFromHeight;
View mView;

public FadeUpAnimation(View view) {
    this.mView = view;
    this.mFromHeight = view.getHeight();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    int newHeight;
    newHeight = (int) (mFromHeight * (1 - interpolatedTime));
    mView.getLayoutParams().height = newHeight;
    mView.setAlpha(1 - interpolatedTime);
    mView.requestLayout();
}

@Override
public void initialize(int width, int height, int parentWidth,
        int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
}

@Override
public boolean willChangeBounds() {
    return true;
}
}

Then this is how I'm using it

View tv = ...
Animation a = new FadeUpAnimation(tv);
a.setInterpolator(new AccelerateInterpolator());
a.setDuration(300);
tv.setAnimation(a);
tv.startAnimation(a);

You can play with it to see if you can get it to fit your needs.

like image 177
devnate Avatar answered Nov 15 '22 07:11

devnate


Will you use the clicked item again? If not, then you could

  1. Wait until the animation finishes
  2. Delete the item from wherever you store the underlaying data
  3. Then call the notifyDataSetChanged() method of your listadapter
like image 3
Balázs Édes Avatar answered Nov 15 '22 07:11

Balázs Édes