RecyclerView иногда сбивает позицию после invalidate pagingSource

Есть RecyclerView , где данные загружаются постранично через адаптер пейджинга, после нажатия кнопки обновления я хочу обновить видимые элементы, загрузив нужные страницы из API. По какой-то причине в некоторых моментах сбивается позиция в RV. Например, это можно увидеть, если встать на элемент 2,10:50: CRS-13 и нажать обновить

class MainActivity : AppCompatActivity() {

    private val viewModel: MainViewModel by viewModels()

    private val launchesPagingAdapter: LaunchesPagingAdapter by lazy {
        LaunchesPagingAdapter()
    }

    private lateinit var recyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        recyclerView = findViewById<RecyclerView>(R.id.rv_items).apply {
            adapter = launchesPagingAdapter
        }

        findViewById<Button>(R.id.btn_refresh).apply {
            setOnClickListener {
                viewModel.refresh()
            }
        }



        lifecycleScope.launch {
            viewModel.launchesPagingData.collect { pagingData ->
                launchesPagingAdapter.submitData(pagingData)
            }
        }
    }

}
class MainViewModel : ViewModel() {

    private var launchesPagingSource: LaunchesPagingSource? = null
        get() {
            if (field == null || field?.invalid == true) {
                field = LaunchesPagingSource()
            }
            return field
        }

    val launchesPagingData = Pager(
        PagingConfig(pageSize = 20, initialLoadSize = 20, prefetchDistance = 10)
    ) {
        launchesPagingSource!!
    }.flow
        .cachedIn(viewModelScope)

    fun refresh() {
        launchesPagingSource?.invalidate()
    }

}

как сделать чтоб позиция не сбивалась? пример кода для демонстрации проблемы https://github.com/Vissarion-dev/paging_test


Ответы (0 шт):