ListView 动画单个项目

13
我有一个包含多个项目的ListView。 当用户点击一个项目时,该项目的高度应缩小为零,下面所有项目应向上滚动。 我的代码无法实现这个效果。 使用我的代码,被点击的项目会正确缩放,但是下面的项目没有滚动,它们保持在原来的位置。 我也尝试了使用LinearLayout,但是问题依然存在。
有一个可以正常实现这个功能的应用程序,它叫做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
很难理解你在问什么。 - Arif Nadeem
3个回答

7

这是我制作的一个类(修改自我在这里找到的源代码),它可能会给您想要的功能。

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

您可以尝试使用它,看看是否能够满足您的需求。


嘿,我正在使用这个来折叠列表项,但是当用户按撤销时,我需要项目回来。有什么办法可以反转动画吗?我试过 'newHeight = (int) (mFromHeight * (1 + interpolatedTime));' 将减号改为加号,但那会产生奇怪的效果。 - Zen
interpolatedTime 插值从 0 到 1,因此移除 0 - 应产生相反的效果。您还需要更改 mFromHeight(可能已重命名为 mToHeight)以初始化为视图的自然高度。例如:this.mToHeight = view.getMeasuredHeight(); - devnate
一个注意点:如果高度设置为0,视图将再次占据其完整的高度(至少在我的代码中是这样),因此请执行类似于mView.getLayoutParams().height = Math.max(newHeight, 1);的操作。 - paulgavrikov
太好了!但是我如何缩小列表项内容呢? - Konstantin Konopko

3

您是否会再次使用已单击的项目?如果不是,则可以执行以下操作:

  1. 等待动画完成
  2. 从存储基础数据的任何位置删除该项目
  3. 然后调用列表适配器的notifyDataSetChanged()方法

使用附加到动画的动画监听器来执行“onAnimationEnd”,然后按建议操作。 - Alex Hart
这并没有回答问题-当目标视图缩成无时,较低的视图不会上移,它们只会在动画完成后立即对齐到位置。 - Tom

0

Android不像HTML那样,当您更改视图中的宽度、高度或可见性时,其他视图不会更新其位置。 如果您需要这样做,您必须单独更新所有listview及其内部的视图。这是一段复杂的代码。


错误的。正如@devnate的回答中所示,您可以非常简单地强制更新视图边界。 - paulgavrikov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接