This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ScrollToTopDataObserver(val layoutManager: LinearLayoutManager, | |
| val recyclerView: RecyclerView) | |
| : RecyclerView.AdapterDataObserver() { | |
| override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { | |
| super.onItemRangeInserted(positionStart, itemCount) | |
| val lastVisiblePosition = layoutManager.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. | |
| if (lastVisiblePosition == -1 || positionStart >= itemCount - 1 && lastVisiblePosition == positionStart - 1) { |