仅当ListView向下滚动时Android动画

时间:2016-02-25 10:01:48

标签: android android-layout user-interface listview android-animation

我在布局中有一个ListView, 只有当列表向下滚动时,我才需要应用动画(从下到上显示)。

我该怎么做?

请帮帮我。

2 个答案:

答案 0 :(得分:4)

希望这会帮助你。 谷歌加动画

up_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="100%" android:toYDelta="0%"
        android:duration="400" />
</set>

down_from_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="-100%" android:toYDelta="0%"
        android:duration="400" />
</set>

代码:

private int lastPosition = -1;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Load your view, populate it, etc...
    View view = ...;

    Animation animation = AnimationUtils.loadAnimation(getContext(), (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
    view.startAnimation(animation);
    lastPosition = position;

    return view;
}

答案 1 :(得分:1)

将动画添加到特定视图(可能是ImageView,LinearLayout ......),动画将在getView方法调用时通过适配器膨胀任何新项目时启动

 @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        setAnimation(YOUR_VIEW, position);
    }

 private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition) // you can put any rule you need 
        {
            Animation animation = AnimationUtils.loadAnimation(_Context, R.anim.push_left_in); // add any animation you want 
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }