2012-08-17 84 views
13

我收到了一个带有项目的ListView。 当用户点击一个项目时,它的高度应该缩放为零,并且下面的所有项目都应该向上滚动。 我的代码在下面不起作用。 随着我的代码被点击的项目缩放正确,但下面的项目不向上滚动,他们留在相同的位置。 我也试过用LinearLayout,但也有同样的问题。ListView动画单个项目

有一个应用程序可以做到这一点。它叫做Tasks

This little image should explain the problem

我当前的实现看起来是这样的:

@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> 

+1

你目前的实现是什么样的? – wsanville 2012-08-17 16:38:11

+0

@wsanville新增源码 – 2012-08-17 16:42:04

+0

它真的很难理解你所要求的 – 2012-08-17 17:15:26

回答

7

这里的我做的一个类(从source I found here修改),可能会给你你想要的功能。

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

那么这就是我如何使用它

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

你可以用它玩,看看你能得到它,以适应您的需求。

+0

嘿,我正在使用它来折叠listitem,但是当用户按下撤消时,我需要项目回来。任何方式我可以扭转动画?我试过'newHeight =(int)(mFromHeight *(1 + interpolatedTime));'将minus改为+但这会产生奇怪的效果 – Zen 2014-04-14 06:32:41

+0

'interpolatedTime'从0到1插值,所以删除'0-'应该产生相反的效果。您还必须更改'mFromHeight'(可能重命名为'mToHeight')以初始化为视图的自然高度。例如'this.mToHeight = view.getMeasuredHeight();' – devnate 2014-04-24 03:01:30

+0

有一件事:如果height设置为0,视图会再次将其全高(至少使用我的代码),所以请执行诸如'mView.getLayoutParams()。height =数学。max(newHeight,1);' – bluewhile 2014-08-24 13:29:00

0

而Android不会像HTML,当你改变宽度,高度或视图中的可见性,其他视图不会更新他们的位置。 如果你需要这样做,你必须单独更新所有的listview和你的视图。这是一个复杂的代码。

+0

错误。正如你可以在@devnate中看到的答案,你可以强制查看边界更新非常简单。 – bluewhile 2014-08-24 13:27:33

3

你会再次使用点击的项目吗?如果没有,那么你可以

  1. 等到动画结束
  2. 无论你存储在垫层数据删除项目
  3. 然后打电话给你的listadapter的notifyDataSetChanged()方法
+0

这是正确的。使用附加到“onAnimationEnd”动画的动画监听器,然后按照建议进行操作。 – 2013-09-10 12:45:33

+0

这并没有回答这个问题 - 当目标视图缩放为空时,较低的视图不会向上移动,只有在动画结束后,它们才会卡入到位。 – Tom 2014-11-25 23:01:22