본문

170904(월) - RxJava2 (Infinite scroll with RxJava2, Kotlin)

RxJava2 (Infinite scroll with RxJava2, Kotlin)


Create 사용

Flowable.create({ emitter ->
applicationInfoList
.filter { !it.packageName.isNullOrEmpty() && it.icon != 0 }
.filter { applicationInfoList.indexOf(it) in startIndex until startIndex + MAX_LOADING_APP_LENGTH }
.forEach {
emitter.onNext(App(it.packageName, it.icon, appListActivity.packageManager.getApplicationLabel(it).toString()))
}

appListStartIdx += MAX_LOADING_APP_LENGTH
}, BackpressureStrategy.BUFFER)


fromIteratorable 사용

Flowable.fromIterable(applicationInfoList)
.filter { !it.packageName.isNullOrEmpty() && it.icon != 0 }
.filter { applicationInfoList.indexOf(it) in startIndex until startIndex + MAX_LOADING_APP_LENGTH }
.map { appInfo -> App(appInfo.packageName, appInfo.icon, appListActivity.packageManager.getApplicationLabel(appInfo).toString()) }
.doOnSubscribe { appListStartIdx += MAX_LOADING_APP_LENGTH }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.onBackpressureBuffer()


addOnScrollListener

recyclerview_app_list_activity.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

val totalItemCount = appListStaggeredGridLayoutManager.itemCount
val visibleItemCount = appListStaggeredGridLayoutManager.childCount
val pastVisibleItems = appListStaggeredGridLayoutManager.findFirstVisibleItemPositions(null)[0]

if (visibleItemCount + pastVisibleItems + AppListPresenter.ADJUSTED_VALUE >= totalItemCount) {
appListPresenter.getInstalledAppsByParts(appListPresenter.appListStartIdx).subscribe({ app -> appListAdapter.addApp(app) })
.let { if (totalItemCount == appListPresenter.applicationInfoList.size) it.dispose() }
}
}
})


개선


fromIteratorable 사용

override fun loadMoreImages(page: Long): Disposable = imageLocalDataSource
.loadThumbImages()
.buffer(15)
.elementAt(page)
.doOnSubscribe { imageListActivity.isMoreLoading = true }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
imageListActivity.apply {
addImagesToRecyclerView(it)
isMoreLoading = false
}
},
{
imageListActivity.isMoreLoading = false

Log.e("$javaClass [loadMoreImages()] : ", "${it.printStackTrace()}")
},
{ imageListActivity.isMoreLoading = false })

OnScrollListener

override fun onScrollAndMoreListener() =
object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

if (isMoreLoading) return

imageListGridLayoutManager.apply {
if (childCount + findFirstVisibleItemPositions(null).first() + ImageViewerConstant.PLACEHOLDER_DISTANCE >= itemCount) {
compositeDisposables.add(imageListPresenter.loadMoreImages(page++))
}
}
}
}


'Mobile > RxJava2' 카테고리의 다른 글

171211(월) - @Target  (0) 2017.12.11
171208(금) - @Qualifiers  (0) 2017.12.08
171206(수) - @Scope, @Retention  (0) 2017.12.06
160812(금) - RxJava  (0) 2016.08.12
160802(화) - RxJava(1. 닷넷에서 Rx자바까지)  (0) 2016.08.03

공유

댓글