in build-android/app/src/main/java/com/google/firebase/codelab/friendlychat/MyScrollToBottomObserver.kt [27:39]
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
val count = adapter.itemCount
val lastVisiblePosition = manager.findLastCompletelyVisibleItemPosition()
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
val loading = lastVisiblePosition == -1
val atBottom = positionStart >= count - 1 && lastVisiblePosition == positionStart - 1
if (loading || atBottom) {
recycler.scrollToPosition(positionStart)
}
}