Skip to content

Instantly share code, notes, and snippets.

View carrasc0's full-sized avatar
💭
I may be slow to respond.

Gabriel Betancourt carrasc0

💭
I may be slow to respond.
  • Freelance
  • Sofía, Bulgaria
View GitHub Profile
@danielgomezrico
danielgomezrico / ScrollToTopDataObserver.kt
Created July 15, 2016 04:00
Android / Kotlin - Scroll recycler view automatically based on initial items and scroll to bottom on new items (like a chat room)
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) {