question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Handling clicks in RecyclerView Adapter

See original GitHub issue

Hello,

I was wondering How can I handle clicks from a view inside a RecyclerView Adapter and later collect them outside this adapter for instance in a Fragment/Activity? I’m probably looking for something like PublishSubject in RxJava

For instance

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DiscoverHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.section_title, parent, false)
        view.clicks().collect() //How can I somehow propagate those clicks to a property?
        ..............
    }

I used to do this with callbacks but I want to achieve a similar thing using this library.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ychescale9commented, Feb 9, 2020

Oh now I remember Subject is both a Subscriber and an ObservableSource😃

You can implement a similar API with Flow with an extension function:

fun <T> Flow<T>.transferToChannel(channel: Channel<T>): Flow<T> = onEach {
    channel.offer(it)
}

itemView.clicks().map { myItem }
            .transferToChannel(itemClicksChannel)
            .launchIn(coroutineScope)

Only saved a few characters though:)

1reaction
ychescale9commented, Feb 8, 2020

The equivalence of RxJava Subject in Coroutines is Channel.

You could use a Channel in your adapter to collect the view.clicks() Flows emitted by each item, and expose it as a single itemClicks: Flow<MyItem> for Fragment / Activity to consume:

class MyListAdapter(
    private val coroutineScope: CoroutineScope // pass in the `lifecycleScope` from Activity / Fragment
) : ListAdapter<MyItem, MyViewHolder>(diffCallback) {

    private val itemClicksChannel: Channel<MyItem> = Channel(Channel.UNLIMITED)
    val itemClicks: Flow<MyItem> = itemClicksChannel.consumeAsFlow()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        ...
        return MyViewHolder(view, itemClicksChannel, coroutineScope)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
}

class MyViewHolder(
    private val view: View,
    private val itemClicksChannel: Channel<MyItem>,
    private val coroutineScope: CoroutineScope
) : RecyclerView.ViewHolder(view) {

    fun bind(myItem: MyItem) {
        ....
        view.clicks().onEach {
            clickChannel.offer(myItem)
        }.launchIn(coroutineScope)
    }
}

In Activity / Fragment:

...
myAdapter = MyListAdapter(lifecycleScope)
myAdapter.itemClicks.onEach { myItem ->
    // handle clicked item
}.launchIn(lifecycleScope)
Read more comments on GitHub >

github_iconTop Results From Across the Web

RecyclerView Item Click Listener the Right Way - Stack Overflow
A better way to handle clicks in ViewHolder . See the below example. class Holder extends RecyclerView.ViewHolder implements View.
Read more >
RecyclerView Item Click Listener Best Practice - YouTube
Hello World, today we will take a quick look at what I consider to be the best practice when handling setting OnClickListener for...
Read more >
The Modern Approach to handle Item Click on Recyclerview
The better approach is to set a click listener in onCreateViewHolder which invokes only once when a ViewHolder gets created. Now the question...
Read more >
Android nested RecyclerView handling clicks, using OOP
I wanted to know when a user clicks on any View used in the Nested RecyclerView. For example, if the user clicks on...
Read more >
Handling RecyclerView Clicks the Right Way using Kotlin
In this step, we will create our Adapter class that will do all the click operations. First, we create a class called OnClickListener...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found