vibhor
vibhor

Reputation: 145

Animate only last item of recycler view every time user scroll to end and make it visible

I want to animate appearance of only last item of a recyclerview. Every time scroll is done to end, view should be shown with animation.

Applying Item Animator animates when item is removed, added, deleted. But i did not get option to animate in on scroll.

I applied animation in onBindView, but onBindView is not always called and animation is not started. Moreover, in cases onBindView is called and user is performing slow scroll operation, animation had already started when view is actually visible to user.

What can be suitable way to apply this animation?

Thanks Vibhor

Upvotes: 2

Views: 1260

Answers (1)

Bishoy Kamel
Bishoy Kamel

Reputation: 2355

I have tested this solution and It's working like a charm.

enter image description here

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                LinearLayoutManager layoutManager = LinearLayoutManager.class.cast(recyclerView.getLayoutManager());
                int lastItem=adapter.getItemCount()-1;
                tryAnimation(layoutManager.findViewByPosition(lastItem));
            }
        });



 public void tryAnimation(View view) {
    Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
    if (view != null)
        view.startAnimation(animation);
}

this solution can be optimized by taking advantage of dy

Upvotes: 3

Related Questions