Added scrolling to latest items while flashing

Since the adapter might be set _after_ the request, as there is no guaranteed order, it's done with waiting recursion yuck
This commit is contained in:
Viktor De Pasquale 2019-05-02 16:23:20 +02:00 committed by topjohnwu
parent 09d531557d
commit 4496f82d5b
2 changed files with 40 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import androidx.drawerlayout.widget.DrawerLayout
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.google.android.material.navigation.NavigationView
import com.skoumal.teanity.extensions.subscribeK
@ -141,4 +142,41 @@ fun setSelectedItemPositionListener(view: Spinner, listener: InverseBindingListe
@BindingAdapter("onTouch")
fun setOnTouchListener(view: View, listener: View.OnTouchListener) {
view.setOnTouchListener(listener)
}
@BindingAdapter("scrollToLast")
fun setScrollToLast(view: RecyclerView, shouldScrollToLast: Boolean) {
fun scrollToLast() = view.post {
view.scrollToPosition(view.adapter?.itemCount?.minus(1) ?: 0)
}
fun wait(callback: () -> Unit) {
Observable.timer(1, TimeUnit.SECONDS).subscribeK { callback() }
}
val tag = RecyclerView::class.java.name.sumBy { it.toInt() }
fun RecyclerView.Adapter<*>.setListener() {
val observer = object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
scrollToLast()
}
}
registerAdapterDataObserver(observer)
view.setTag(tag, observer)
}
fun RecyclerView.Adapter<*>.removeListener() {
val observer = view.getTag(tag) as? RecyclerView.AdapterDataObserver ?: return
unregisterAdapterDataObserver(observer)
}
fun trySetListener(): Unit = view.adapter?.setListener() ?: wait { trySetListener() }
if (shouldScrollToLast) {
trySetListener()
} else {
view.adapter?.removeListener()
}
}

View File

@ -96,9 +96,11 @@
<androidx.recyclerview.widget.RecyclerView
itemBinding="@{viewModel.itemBinding}"
items="@{viewModel.items}"
android:id="@+id/flash_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
scrollToLast="@{true}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_console" />