我有以下代码来动画RecyclerView项目。这非常好,但如果我向下滚动它会动画出现的所有项目。
我想要的是Google Play音乐应用中的行为,其中动画仅针对最初可见的项目进行播放。通过滚动显示的项目应显示为没有动画适配器。知道如何归档这种行为吗?
for(int one = 0; one <= num2; one++){
ta1.setText(ta1.getText()+String.valueOf("X"));
}
答案 0 :(得分:2)
我在Android Studio中测试了这个方法并且它有效,它也应该适合你一些小调整:
class YourAdapter extends RecyclerView.Adapter{
private int lastPosition = -1;
private boolean isAnimation;
YourAdapter(... )
{
...
this.isAnimation = true;
}
....
private void switchAnimation(boolean isAnimation)
{
this.isAnimation = isAnimation;
}
private void animateItem(int position, View viewToAnimate)
{
if (position > m_LastPosition && isAnimation)
{
var animation = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.slide_in_bottom);
viewToAnimate.StartAnimation(animation);
lastPosition = position;
}
}
}
然后在你的LayoutManager中覆盖方法onLayoutChildren
(你必须将代码容纳到C#):
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false){
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
myAdapter.switchAnimation(false);
}
};
如果有帮助,请告诉我;)