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 theUI
thread from background (e.g. inside a method which will be run in a background thread).
for more you run it onUI
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
Javamail API in Android Using Xoauth
Android App Integrated with Ok Google
Check Whether Lock Was Enabled or Not
Android.Content.Res.Resources$Notfoundexception: Unable to Find Resource Id #0Xffffffff
Android Accessibility Service Detect Notification
Android: Broadcastreceiver Intent to Detect Camera Photo Taken
How to Create Multiple Pendingintents with the Same Requestcode and Different Extras
How to Unpair Bluetooth Device Using Android 2.1 Sdk
Searchview in Optionsmenu Not Full Width
Passing JSONobject into Another Activity
Opening Infowindow Automatically When Adding Marker Google Maps V2 Android
Delete File from Internal Storage
Android Get a Cursor Only with Contacts That Have an Email Listed >Android 2.0
Getting All the Total and Available Space on Android
Could Not Find Com.Android.Tools.Lint:Lint-Gradle Android Studio 3