I'm quite new to RxJava and have a problem with customizing my flow. The use case here is:
I have a RecyclerView
with a GridLayoutManager
that displays a grid of images and a footer with a TextView
that will display the total amount of images. To put it into a context, me as a user can have a total of 15 images, reflected by the TextView
which will say "15 images", but the grid will still show only 4 (understand it as a "preview"). Now, to show these images, in onComplete()
of the Observable that I create, I will call adapter.notifyDatasetChanged()
. However, its pointless to wait for these images to appear, if the list consists of, lets say, 200 images. I would like to notify the adapter as soon as 4 images are emitted from the list. My observable is created in that way:
Observable.just(user.getUserId())
.subscribeOn(Schedulers.io())
.map(this::getImageList)
.flatMap(images -> Observable.fromIterable(images)
.map(image -> image))
.observeOn(AndroidSchedulers.mainThread());
Having done so, I can't notify the adapter in Observer.onNext()
, because if the images.size == 200
then it will be notified 200 times (hope I'm correct here). I can't do it in Observer.onComplete()
neither as it will wait for all of the 200 exemplary images to be emitted before the adapter gets notified. How can I intercept that flow, do an action (notify the adapter) when 4 items from the list have been emitted, but continue emitting the rest, so I can show the proper footer text with the total images amount, without completing with only 4 images?
UPDATE - for purposes outside of the scope of this question, I need the full list of images after the observable completes. I want to display 4 of them, update the TextView
with the total amount of images retrieved AND store the list of total images retrieved.