Is PublishSubject is alternative of Interface?

Shivam Sharma
3 min readSep 9, 2020

--

Question is:-

Can we use PublishSubject as an alternative of Interface?

The answer is:- Yes, we can use it in some specific places, not all places.

First, we have to understand what is Subject? I am not going to give an explanation on this, please read from here.

At some point, we can use Publish Subject as an alternative of Interface like getting a callback from the adapter class or getting a callback from some custom view that you created.

So in this article, I am going to explain how to use PublishSubject to get a callback from an adapter class.

What is PublishSubject?

PublishSubject is a kind of thing, who can emit the values from different places and the same who can consume it in different places.

PublishSubject is part of RX so we need to add the following dependency for PublishSubject in Gradle:-

//RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.2.7'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

What we are going to todo?:-

First, we have to create a class where we need to show RecyclerView that contains a list of Car's name. On click on an item in recyclerView show Toast on-screen with that clicked car’s name that’s it, but using PublishSubject.

Now going to create the main file where we need to show this recyclerView.

Create Adapter class and layout file:-

Creating a ViewHolder class:-

Now Magic begin from here:-

In Adapter class need to do 3 steps:-

1:- Initialize PublishSubject at global level

private val publishSubject = PublishSubject.create<String>()

2:- Pass that variable of PublishSubject to ViewHolderClass

CarsViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_item, parent, false), publishSubject)

3:- Create a getter of PublishSubject varibale

fun getPublishSubject(): PublishSubject<String> {
return publishSubject
}

Adapter class now looks like as below:-

Now Moved to the next part CarsViewHolder

When you added the PublishSubject variable in CarsViewHolder from Adapter class, So here we need to update in CarsViewHolder class as expected and initialize the local publishSubject object with pass value from Adapter class.

class CarsViewHolder(itemView: View, publishSubject: PublishSubject<String>) : RecyclerView.ViewHolder(itemView) {

var publishSubject: PublishSubject<String>? = null
init {
this.publishSubject= publishSubject
}
}

We pass the publishSubject object from Adapter now time to use that PublishSubject object to emit that value from ViewHolder class.

In bindData method we need to add setOnClickListener on txtName and emit that value from onNext(); like below:-

fun bindData(value: String) {
itemView.txtName.text = value
itemView.txtName.setOnClickListener {
publishSubject!!.onNext(value)
}
}

Full code for CarsViewHolder class

Till now we have done 2 things we created PublishSubject and we emit the value. Only the last one thing is left to Consume the values from PublishSubject.

In Fragment class we have to consume that value. Guys we created getter in Adapter now this time we can use that getter to consume PublishSubject to get value from RecyclerView that emit in ViewHolder class.

Make sure the adapter is not null. Add the following code to consume the value;-

private fun setObserver() {
adapter.getPublishSubject().subscribe(
this::onItemClick
)
}

fun onItemClick(value: String): Unit {
Toast.makeText(activity,value, Toast.LENGTH_SHORT).show()
}

Add setObserver() method onViewCreated() after the initialization of adapter.

In the above code snippet, I am using the lambda function to reduce the number of line codes.

The whole code of this class is below:-

The final output is as follow:-

Here we have done all things very easily, Hope you guys understand a little bit How PublishSubject work for emitting or consuming value from a different place.

Once you all done this, In this case, no need to create an Interface to get a callback from recyclerView onClickListener().

Please Checkout my Github repo of this example:-

Hope you guys enjoy it. If this article helps you don’t forget 👏🏻 for this post.

Happy coding guys 😎

--

--

Shivam Sharma
Shivam Sharma

No responses yet