2

When I am using androidx ListAdapter with ViewPager2 and try to update the specific list item from fragment with adapter.notifyItemChanged, I have crash that shows java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling.

Previously I was using recyclerView and could resolve the crash by the condition with if(recyclerView.isComputingLayout()) before adapter.notifyItemChanged. Unfortunately ViewPager does not have method that returns computing information. Also, checking the stateScroll gives me nothing. Maybe someone had this problem before? Here's code:

viewModel.invoicePositionList.observe(viewLifecycleOwner, {
        if (viewModel.currentInvoicePositionPosition >= 0) {
            adapter.notifyItemChanged(viewModel.currentInvoicePositionPosition)
            viewModel.currentInvoicePositionPosition = -1
        } else {
            adapter.submitList(it)
        }
    })
tr0lczyk
  • 35
  • 1
  • 10
  • try answers from [here](https://stackoverflow.com/questions/43221847/cannot-call-this-method-while-recyclerview-is-computing-a-layout-or-scrolling-wh) – Zain Nov 02 '20 at 20:32
  • Hey, I tried the post solution previously. It works at first, however then after a new item is added to list the recycler/viewPager is staggering, working slowly and frames are visible :/ – tr0lczyk Nov 02 '20 at 21:14

1 Answers1

0

You are not expected to call notifyItemChanged when using ListAdapter. All you need is to create a new list without the removed item and call submitList.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45