1

I have a RecyclerView.Adapter where list items are being removed, added and/or updated dynamically in the background, and the individual effects for this actions also work using notifyItemRemoved, notifyItemInserted and notifyItemChanged. But I have the problem that there is the case where e.g. an item is added dynamically to the end of the list at a position not currently visible in screen and in this special scenario I would like to shortly animate the complete list, so the user can know that the list changed, is there some specific guidelines for doing so, or some example how to handle this appropriately? I tried calling notifiyDataSetChanged after calling e.g. notifyItemRemoved but this cancels the effect of notifyItemRemoved and there isn't really a visible feedback.

David
  • 3,971
  • 1
  • 26
  • 65
  • Why dont you check inside notifyItemInserted, if the item inserted is visible or not? – Ricardo Aug 03 '17 at 16:03
  • Inside notifyItemInserted? And how would you do tthis? I am not aware of a function call inside the recycleView adapter to do this. – David Aug 03 '17 at 16:30

1 Answers1

0

I'm not sure which kind of animation you'd like to achieve, but something simple is: pass as a parameter to the Adapter the RecyclerView reference and do the animation there after you notifyItemInserted, e.g. scroll the RecyclerView's Layout to the end to show the user the new item using recyclerView.getLayoutManager().scrollToPosition(youPositionInTheAdapter)

Ohh and for animating the items you can also use this library https://github.com/wasabeef/recyclerview-animators

Update

In order to set a "flash" animation to the whole RecyclerView you could use after you notifyItemInserted

Animation animation = new AlphaAnimation(1.0f, 0.0f);

animation.setDuration(500);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.REVERSE);

LayoutAnimationController controller = new LayoutAnimationController(myAnim, 0.01f);
recyclerView.setLayoutAnimation(controller); 
fmaccaroni
  • 3,846
  • 1
  • 20
  • 35
  • The question was more general if they are maybe some recommended aproaches for this case and how to do it, I was thinking on "flashing" the list shortly, scrolling is not something I would want to. – David Aug 03 '17 at 16:29
  • Ok, the animation starts automatically with this, for the ideal solution I would need to start the animation only if the added/updated/removed item was at a list position currently outside the screen, but I think your answer is valid – David Aug 03 '17 at 18:12
  • Just check before starting the animation if the position of the new inserted item is greater than the one given by `findLastVisibleItemPosition` of LayoutManager https://stackoverflow.com/questions/24989218/get-visible-items-in-recyclerview – fmaccaroni Aug 03 '17 at 18:18
  • What I did not get yet an maybe also interesting for others reading this solution, does setLayoutAnimation also starts the animation automatically and means that the animation will start each time a change in the adapter is made? And how to manually start the animation? – David Aug 04 '17 at 06:22
  • For others reading, findLastVisibleItemPosition is not reliable after adding or removing items from the adapter, if last position is e.g. 4 and you insert two items before position 4 the function will reutrn 6 despite calling all type of refresh method no matter if in the adapter/layoutmanager or recyclerview. – David Aug 04 '17 at 08:37