Issue
I have the following code to animate RecyclerView items. This works very good but it animates all items that are appearing if I scroll down.
What I want is an behaviour like in Google Play Music app where the animation is only played for the items that are initially visible. The items that are getting visible through scrolling should appear as there is no Animation Adapter. Any idea how to archive this behaviour?
public class RecyclerViewAnimationAdapter : RecyclerView.Adapter
{
private int m_LastPosition = -1;
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);
animateItem(position, holder.ItemView);
}
public override void OnViewDetachedFromWindow(Object holder)
{
base.OnViewDetachedFromWindow(holder);
var viewToAnimate = ((RecyclerView.ViewHolder) holder).ItemView;
viewToAnimate.ClearAnimation();
}
private void animateItem(int position, View viewToAnimate)
{
if (position > m_LastPosition)
{
var animation = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.slide_in_bottom);
viewToAnimate.StartAnimation(animation);
m_LastPosition = position;
}
}
}
Solution
I tested this method in Android Studio and it worked, it should work for you too with some little tweaks:
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;
}
}
}
Then in your LayoutManager override the method onLayoutChildren
(You have to accomodate the code to 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);
}
};
Let me know if that helped ;)
Answered By - nabil london
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.