RecyclerView item click using RxJava2
Shivam Chopra
1663

Thanks for writing the article. I think it’s going to be useful for many. Just some suggestions/opinion I think would be nice to add to make the code a bit neater and more comply to RxJava style:

  1. You can use RxBinding by Jake Wharton (which turns View events into RxJava streams) to get rid of the clickListeners. For example, your itemView ’s click listener code would become: itemView.clicks().map { items[layoutPosition] }.subscribe(clickSubject). Now everything is in a single stream.
  2. Also you can get rid of the getClickListener() function in Adapter by turning it into a variable:
val clickEvents: Observable<String> = clickSubject

and use it like this:

disposable = adapter.clickEvents.subscribe {                      Toast.makeText(this, "Clicked on $it", Toast.LENGTH_LONG).show()                }

That’s more about personal preferences. One may like or dislike it. But I thought its good to share.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.