动画列表仅在孩子出现时查看

Nav*_*deh 7 animation android listview android-listview

我有一个listView,大约有20个项目(动态项目).我想先向用户展示这些项目的动画.像Google+卡片.我想要实现一些要点:

  1. 只有在用户开始看到它们时才会设置动画.
  2. 项目仅动画一次.(不是每次用户看到它们)
  3. 快速滚动项目不要互相混淆.
  4. 动画根据项目的位置开始延迟.

到目前为止,我尝试过:

  • LayoutAnimationController(此方法不符合第一个要求)
  • convertView.startAnimation(此方法不符合第二个要求)
  • convertView.startAnimation带有一个标志,如果该位置的项目之前已经过动画处理(此方法不适用于listView中的第一项,因为,对于listView中的第一项,getView方法将被调用两次.(我不是知道原因!layout_height和layout_width都是match_parent))

我搜索了很多但是没有找到解决方案.

顺便说一句,我不想​​使用外部库.我之前见过这个.

谢谢.

Sim*_*mas 8

我刚试过这个,似乎满足了你的所有要求:

boolean[] animationStates;

public void YourConstructor(...) {
    ...
    animationStates = new boolean[data.size()];
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // (Re)Use the convertView
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.popup_list_item, parent, false);
        if (!animationStates[position]) {
            Log.e("TAG", "Animating item no: " + position);
            animationStates[position] = true;
            Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
            animation.setStartOffset(position*500);
            convertView.startAnimation(animation);
        }
    }
    // Use convertView here
    return convertView;
}
Run Code Online (Sandbox Code Playgroud)

如果您有兴趣,这是我的fade_in.xml文件:

<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>
</set>
Run Code Online (Sandbox Code Playgroud)