What's better? notifyDataSetChanged or notifyItemChanged in loop?
If you are simply updating one part of the view, use the notifyItemRangeChanged()
or notifyItemChanged()
instead of notifiyDataSetChanged()
. The difference here has to do with structural changes vs item changes. This is on the android developers RecyclerView.Adapter
documentation found here.
Here is another tidbit on the differences between the two types of changes:
There are two different classes of data change events, item changes
and structural changes. Item changes are when a single item has its
data updated but no positional changes have occurred. Structural
changes are when items are inserted, removed or moved within the data
set.
This is taken from that aforementioned page,
If you are writing an adapter it will always be more efficient to use
the more specific change events if you can. Rely on
notifyDataSetChanged() as a last resort.
So just to clarify use notifyDataSetChanged()
as a last resort, and instead ask yourself if you can preform one of these methods instead, and if you can use it instead:
notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)
which makes sense because notifyDataSetChanged()
will pretty much try to redraw everything based on the data and make no previous assumptions on it, while the other methods will just look for changes. That means the adapter has to do a lot more work that is not necessary. This is what notifyDataSetChanged()
does:
This event does not specify what about the data set has changed,
forcing any observers to assume that all existing items and structure
may no longer be valid. LayoutManagers will be forced to fully rebind
and relayout all visible views.
This also makes sense to use the incremental or range approach, because you are changing the text, you need to go get each new text and when you do that you should tell the adapter you changed it. Now, if you do a button click and get all new text values, and create a new list or something then call heavy notifyDataSetChanged()
.
Why RecyclerView.notifyItemChanged() will create a new ViewHolder and use both the old ViewHolder and new one?
RecyclerView use both of ViewHolder for smooth animation from an old state to a new. This is default behaviour of RecyclerView.ItemAnimator.
You can disable animation by passing an empty item animator to RecyclerView:
listView.setItemAnimator(null);
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;
}
Related Topics
Adb Connection by Wifi Getting Killed When a New Usb Attached/Detached
Build Unsigned APK File with Android Studio
What Does It Mean to Inflate a View from an Xml File
How to Round an Image with Glide Library
Sign APK Without Putting Keystore Info in Build.Gradle
How to Make a Actionbar Like Google Play That Fades in When Scrolling
Android Destroying Activities, Killing Processes
Android: What Are the Recommended Configurations for Proguard
How to Get Activity's Content View
How to Check an Android Device Is Hdpi Screen or Mdpi Screen
I Can't Find the Android Keytool
Fragment Transaction Animation: Slide in and Slide Out
Android Animation Does Not Repeat
Android Displaymetrics Returns Incorrect Screen Size in Pixels on Ics
Load an Image from Assets Folder
Android Toggle Button Custom Look
Theme/Style Is Not Applied When Inflater Used with Applicationcontext