0

By using this simple section adapter in my app to make tow sections in RecyclerView ,every thing is work greet but the problem is in the SimpleSectionedRecyclerViewAdapter.java class when called notifyDataSetChanged();

the code of error is :

 mBaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            mValid = mBaseAdapter.getItemCount()>0;
            notifyDataSetChanged();              //<------------------------the error is here
        }

        @Override
        .
        .
        .
    });

in my fragment I added tow type of data unfinished tasks and finished tasks to recyclerview and every section has the size of their items count , the start position of second section will change depend on the unfinished tasks size

for example I have 5 Tasks and all of them is in the unfinished tasks section so by click on item in the RecyclerView the task will be finished and now i need to call notifyDataSetChanged(); so the result will be 4 items in Unfinished Tasks section and 1 item will added to finished task section

every thing work good but the data in recyclerView is not Changed and when I call notifyDataSetChanged(); the app crash and show error :

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView

So I comment the notifyDataSetChanged(); and the data in RecyclerView Not changed accept when I reopen the fragment

Any Help how can I solve this ?

  • This [project](https://github.com/lucasr/twoway-view/issues) seems abandoned - and it might even only build with just as old versions of Android Studio. – Martin Zeitler Dec 12 '21 at 21:02

1 Answers1

0

This method likely causes an event loop and the IDE should indicate a cyclic call.

@Override
public void onChanged() {
     // ever wondered, which event this may trigger?
     notifyDataSetChanged();
}

One simply cannot call notifyDataSetChanged() in that adapter-state / callback. And even if you somehow manage to call it from another thread, it will call itself over and over again... and with every new call, it may only stall more and more.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216