'How to change main scope to another in recyclerview, adapter?

This is my code in adapter using recyclerView. How to improve this code to not use Main Scope, but to use scope that could be disposal? I need to have access to get the item.id, that's why I use it in adapter not in viewmodel/fragment.

 _stateFlow.value = item.id

my code in adapter

// define flowState
(...)
 val _stateFlow = MutableStateFlow(-1)
 val stateFlow = _stateFlow.asStateFlow()

// after every change of element
(...)
                CoroutineScope(Dispatchers.Main).launch {
                    stateFlow.collect { id ->
                        statusButtons.toggleVisibility(item.id == id)
                    }
                }

EDIT1 answer for comment: Without coroutine I can't use collect on stateFlow always when my id is changed. My action is like I need to trigger element previously clicked when I click next one.

without coroutine I an't use collect



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source