下面是我在Adapter 中添加动画的方法。这将为推动效果设置动画,行从右侧进入。
先在xml中定义动画(res/anim/push_left_in.xml)
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="300" />
</set>
然后在你的适配器中设置它
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.music_list_item, null);
} else {
row = convertView;
}
...
//Load the animation from the xml file and set it to the row
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_in);
animation.setDuration(500);
row.startAnimation(animation);
return row;
}
每次添加新行时都会显示此动画,它应该适用于您的情况。
编辑
这是使用RecyclerView 添加动画的方法
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
holder.text.setText(items.get(position));
// Here you apply the animation when the view is bound
setAnimation(holder.container, position);
}
/**
* Here is the key method to apply the animation
*/
private void setAnimation(View viewToAnimate, int position)
{
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition)
{
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.push_left_in);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}