Cannot call this method while RecyclerView is computing a layout or scrolling when try remove item from recyclerview

notifydatasetchanged illegalstateexception
update recyclerview content without refreshing the data
refresh recyclerview after delete item
delete one item from recyclerview
get recyclerview from adapter
recyclerview remove multiple items
override notifydatasetchanged recyclerview
refresh recyclerview on item click

I am Trying to remove my item from recyclerview, but i always getting error

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

i am using notify datasetchanged, can i solve this?

public class AdapterIntransit extends RecyclerView.Adapter<AdapterIntransit.ViewHolder> {
    private Context context;
    List<DataIntransit> data;

    public AdapterIntransit(Context context, List<DataIntransit> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public AdapterIntransit.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardintransit, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(AdapterIntransit.ViewHolder holder, int position) {
        if (data.get(position).getJml1() - data.get(position).getJml2() <= 0) {
            data.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, getItemCount());
            notifyDataSetChanged();
        } else {
            holder.kode.setText(data.get(position).getKode());
            holder.nama.setText(data.get(position).getNama());
            holder.jumlah.setText(String.valueOf(data.get(position).getJml1() - data.get(position).getJml2()));
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView kode, nama, jumlah;
        public ViewHolder(View itemView) {
            super(itemView);
            kode = (TextView)itemView.findViewById(R.id.kode);
            nama = (TextView)itemView.findViewById(R.id.nama);
            jumlah = (TextView)itemView.findViewById(R.id.jumlah);

        }
    }
}

Below answer worked for me

This is just workaround for the problem.

This usually occurs when you are calling notifyDataSetChanged() on the background thread. So just move notify to UI thread

dialeecyclerView.post(new Runnable()
            {
                @Override
                public void run() {
                myadapter.notifyDataSetChanged();
            }
            });

You use your RecyclerView instance and inside the post method a new Runnable added to the message queue. The runnable will be run on the user interface thread. This is a limit for Android to access the UI thread from background (e.g. inside a method which will be run in a background thread). for more you run it on UI thread if you needed.

For more you can run it on UI thread, if you needed

 runOnUiThread(new Runnable(){
 public void run() {
      // UI code goes here
 }
 });

java.lang.IllegalStateException: Cannot call this method while , IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling at android.support.v7.widget.RecyclerView  If your Android app is crashing with error IllegalStateException “Cannot call this method while RecyclerView is computing a layout or scrolling”, you may try placing your notifyitemchanged inside a Runnable.

This is useful when select All checkbox on click of top checkbox in recyclerview.

recyclerview.post(new Runnable()
{
     @Override
     public void run() {
         myadapter.notifyDataSetChanged();
     }
});

Every Rose has its Thorns - Wolox, Cannot call this method while RecyclerView is computing a layout or scrolling #​84 Because it seems to be something that happens when scrolling the list I think I'll just remove the custom SwitchPreferenceCompat class  For calling API after creating the activity, I need to perform click event for RecyclerView item of " 0 " index. for that in onBindviewHolder I'm calling holder.view.performClick event. But the Problem is I need to call a notifydataSetChanged() method in onClick event to update the adapter.

If you are tyring to notify recylew item then use isComputingLayout method:

if (!mRecyclerView.isComputingLayout()) 
{
 // add your code here
}

NotifyDataSetChanged Recycler view : Cannot call this method , It's undeniable that being a brand new widget, RecyclerView has to place a custom layout as a first or last item of RecyclerView. when trying to update the adapter from a callback (for example: trying IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling. PROBLEM: I can't call NotifyDataSetChanged. I have tried every possible thing I've found on the net. ERROR : Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView I have to "manually" refresh the view so that the images are loaded. No problem for the text.

I give you another idea for solve your problem. I think it could be better The idea is we do not remove invalid data inside onBindViewHolder, we will remove it before

public AdapterIntransit(Context context, List < DataIntransit > data) {
    this.context = context;
    this.data = removeInValidData(data);
}

private void removeInValidData(List < DataIntransit > data) {
    for (int position = 0, position < data.size(); position++) {
        if (data.get(position).getJml1() - data.get(position).getJml2() <= 0) {
            data.remove(position);
        }
    }
}

@Override
public void onBindViewHolder(AdapterIntransit.ViewHolder holder, int position) {
    holder.kode.setText(data.get(position).getKode());
    holder.nama.setText(data.get(position).getNama());
    holder.jumlah.setText(String.valueOf(data.get(position).getJml1() - data.get(position).getJml2()));

}

Cannot call notifyDataSetChanged() method while RecyclerView is , Source - Cannot call this method while RecyclerView is computing a layout or scrolling when try remove item from recyclerview · share|improve  Since the GetView() method is called to recycle / repaint the List whenever it goes out of sight. The image tries to redraw itself. To solve my problem I used and external library SmartImageView which allows images to be loaded from URLs and Images are cached to memory and to

You are notifying about item change inside onBindViewHolder() method, when your item in process of construction. Most probably you can just improve your logic, to avoid this.

IllegalStateException: Cannot call this method while RecyclerView is , Nested RecyclerViews : Cannot call notifyDataSetChanged() method while But​, when I do that, I get an exception : Cannot call this method while RecyclerView is computing a layout or scrolling. But let give it a try I. You don't which specific item could be changed. how to remove an item from ListView while getView()?. 1. RecyclerView Search Filter – getFilter() Android provides Filterable class to filter the data by a filter (condition). Usually the getFilter() method has to be overridden in the adapter class in which the filter condition is provided to search through a list.

v7/recyclerview/src/android/support/v7/widget/RecyclerView.java , I am trying to remove an item from my RecyclerView, but I always get an error. Cannot call this method while RecyclerView is computing a layout or scrolling I  Similar to the way we previously used ListViews in conjunction with ArrayAdapters, we'll call the .setAdapter() method on our new RecyclerView to set RestaurantListAdapter as its new adapter. Additionally, we'll need to create and set an instance of the LayoutManager the RecyclerView requires. We'll use the LinearLayoutManager.

RecyclerView.LayoutManager, <li><em>Index:</em> The index of an attached child view as used in a call to Some methods should not be called during these periods (e.g. adapter data change). assertNotInLayoutOrScroll("Cannot remove item decoration during a scroll or" cannot be changed while RecyclerView is trying to compute the layout. A layout defines the visual structure for a user interface, such as the UI for an activity or app widget . You can declare a layout in two ways: The Android framework gives you the flexibility to use either or both of these methods for declaring and managing…

assertNotInLayoutOrScroll("Cannot remove item decoration during a scroll or" throw new IllegalStateException("Cannot call this method while RecyclerView is " cannot be changed while RecyclerView is trying to compute the layout. * <p>.

Comments
  • Possible duplicate of Android RecyclerView addition & removal of items
  • can you explain more about why you need remove item inside onBindViewHolder. I think you can simple don't add it to your RecyclerView
  • @PhanVanLinh, i think because, can access position from onBindViewHolder
  • This seems to be a bug in the RecyclerView library, when notifyDataSetChanged() is called either in quick succession (within 50 ms) or whilst scrolling -- even if it's called outside of onBindViewHolder(). For example, it happens when refreshing the whole list with notifyDataSetChanged(), not necessarily adding or removing items. The Runnable suggestion works well. There is a bug report here: issuetracker.google.com/issues/37136189
  • generally speaking, doing something dependent on time for UI is a bad idea. This will work, but it is a latch rather than a solution.
  • One reason might be that you have a view in your view holder that animates. Maybe it is a switch, and after you switch, you called notify...
  • Thank you sir for your answer, but i can't access position from there.
  • Just the code without the idea why the code could solve the problem is not very helpful. Please also explain the idea behind your solution.
  • @Janusz thank you. updated the answer. I used to think if people have read question + check the log + check my answer, people will get the idea of this answer
  • This solution ONLY works if you want to filter the data at construction of the adapter