Cannot Call This Method While Recyclerview Is Computing a Layout or Scrolling When Try Remove Item from Recyclerview

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

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

recyclerView.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
}
});

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

As Abbas said

This usually occurs when you are calling notify on bg thread. So just move notify to ui thread

else you can use notifyItemChanged(position);

this will also worked as well.

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

Filter the data in list before passing to Adapter

 ArrayList<Dbbean> list = yourlist; 

Create a filtered list

  ArrayList<Dbbean> filteredList = new ArrayList<>();
for(Dbbean dbean : list){
if(dbean.blename.startsWith("Dev")
filteredList.add(dbean);
}

Pass filtered list to Adapter

EvelistAdapter adapter = new EvelistAdaptr(this,filteredList);
yourRecyclerView.setAdapter(adapter);

Android RecyclerView : notifyDataSetChanged() IllegalStateException

You should move method 'setOnCheckedChangeListener()' to ViewHolder which is inner class on your adapter.

onBindViewHolder() is not a method that initialize ViewHolder.
This method is step of refresh each recycler item.
When you call notifyDataSetChanged(), onBindViewHolder() will be called as the number of each item times.

So If you notifyDataSetChanged() put into onCheckChanged() and initialize checkBox in onBindViewHolder(), you will get IllegalStateException because of circular method call.

click checkbox -> onCheckedChanged() -> notifyDataSetChanged() -> onBindViewHolder() -> set checkbox -> onChecked...

Simply, you can fix this by put one flag into Adapter.

try this,

private boolean onBind;

public ViewHolder(View itemView) {
super(itemView);
mCheckBox = (CheckBox) itemView.findViewById(R.id.checkboxId);
mCheckBox.setOnCheckChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!onBind) {
// your process when checkBox changed
// ...

notifyDataSetChanged();
}
}

...

@Override
public void onBindViewHolder(YourAdapter.ViewHolder viewHolder, int position) {
// process other views
// ...

onBind = true;
viewHolder.mCheckBox.setChecked(trueOrFalse);
onBind = false;
}

NotifyDataSetChanged Recycler view : Cannot call this method while Recycler

It is because you're calling notifyDataSetChanged inside your onBindViewHolder like this:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

...

((ItemMessageFriendHolder) holder).avata.setImageBitmap(BitmapFriend);

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

...

}

You shouldn't force the Adapter to refresh itself before it isn't finished binding the view yet.

So, you need to remove the following code from your onBindViewHolder:

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

then just call notifyDataSetChanged() when you need it. For example, when your dataset is changed.



Related Topics



Leave a reply



Submit