1

In my viewModel, I collect a flow

 viewModelScope.launch {
            repository.getMessagesStream(project).cachedIn(viewModelScope).collectLatest {
                _uiState.value = UiState.Content(it)
            }
    }

and submit it to adapter when observed on ui

lifecycleScope.launch {
                adapter.submitData(messageList)
            }

In my adapter, I have a functionality to select/deselect items. The logic involves updating the room database with a boolean value (isSelected).

   fun upDateSelectedMessage(messageId: Int, isSelected: Boolean) {
    viewModelScope.launch {
        withContext(Dispatchers.IO) {
            repository.updateSelectedMessage(isSelected = isSelected, messageId =messageId)
        }
    }
}

Problem:

When I scroll further down the list and select/unselect from there, the list scrolls up to the top. I don't have a network service, Am getting data exclusively from the room database.

fun getMessagesStream(): Flow<PagingData<Message>> {
    val pagingSourceFactory = { appDao.getMessages() }

    return Pager(
        config = PagingConfig(pageSize = 30, enablePlaceholders = false),
        pagingSourceFactory = pagingSourceFactory
    ).flow
}

How can I prevent the scrolling to the top when an item is selected/unselected?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Don Louis
  • 61
  • 1
  • 7

0 Answers0