question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash on data-set update when DiffUtils is used

See original GitHub issue

Issues and steps to reproduce

I’m seeing this crash when trying to filter my list items using a search query:

FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=45; index=-1
    at com.google.android.flexbox.FlexboxLayoutManager$AnchorInfo.assignFromView(FlexboxLayoutManager.java:2863)
    at com.google.android.flexbox.FlexboxLayoutManager$AnchorInfo.access$1900(FlexboxLayoutManager.java:2792)
    at com.google.android.flexbox.FlexboxLayoutManager.updateAnchorFromChildren(FlexboxLayoutManager.java:1136)
    at com.google.android.flexbox.FlexboxLayoutManager.updateAnchorInfoForLayout(FlexboxLayoutManager.java:1032)
    at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:700)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)

Steps to reproduce This started to happen after I added DiffUtils for showing item animations. Here’s what I’m doing:

List<FolderSubscription> oldSubscriptions = ...;
List<FolderSubscription> newSubscriptions = ...;

FolderSubscriptionDiffCallbacks callback = new FolderSubscriptionDiffCallbacks(oldSubscriptions, newSubscriptions);
DiffUtil.DiffResult folderDiffResult = DiffUtil.calculateDiff(callback, true /* detectMoves */);

folderAdapter.updateData(newSubscriptions);
folderDiffResult.dispatchUpdatesTo(folderAdapter);

Version of the flexbox library

I tested on both v0.3-alpha3 and v0.3.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
shrpereiracommented, Dec 10, 2017

I’m having the same issue using Renderers library and their diff algorithm with FlexboxLayoutManager. The lib algorithm is the following:

  DiffCallback diffCallback = new DiffCallback(collection, newList);
  DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
  clear();
  addAll(newList);
  diffResult.dispatchUpdatesTo(this);

Changing from their adapter.diffUpdate(list) to the conventional updating the list and calling adapter.notifyDataSetChanged(); works but I’ve lost the animation.

0reactions
j-garincommented, Jan 21, 2019

@thagikura still getting this FC

Issues and steps to reproduce

Update the items in androidx.recyclerview.widget.RecyclerView with androidx.recyclerview.widget.ListAdapter

Expected behavior

The list should handle multiple changes

Version of the flexbox library

1.1.0

Link to code

		val layoutManager = FlexboxLayoutManager(recyclerView.context)
				.apply {
					flexWrap = FlexWrap.WRAP
					flexDirection = FlexDirection.ROW
					justifyContent = JustifyContent.CENTER
					alignItems = AlignItems.CENTER
				}
		recyclerView.layoutManager = layoutManager
		recyclerView.adapter = adapter

happens when adapter.submitList(newList) gets called often. I have about 10 items in the list in my case, 1 item gets changed per call.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:4941)
        at android.view.ViewGroup.addView(ViewGroup.java:4772)
        at android.view.ViewGroup.addView(ViewGroup.java:4712)
        at androidx.recyclerview.widget.RecyclerView$5.addView(RecyclerView.java:851)
        at androidx.recyclerview.widget.ChildHelper.addView(ChildHelper.java:107)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:8336)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8294)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8282)
        at com.google.android.flexbox.FlexboxLayoutManager.layoutFlexLineMainAxisHorizontal(FlexboxLayoutManager.java:1513)
        at com.google.android.flexbox.FlexboxLayoutManager.layoutFlexLine(FlexboxLayoutManager.java:1434)
        at com.google.android.flexbox.FlexboxLayoutManager.fill(FlexboxLayoutManager.java:1286)
        at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:754)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Smart way to update RecyclerView using DiffUtil - Medium
This class is used to notify updates to a RecyclerView Adapter. It uses Eugene W. Myers's difference algorithm to calculate the minimal number...
Read more >
How to properly use DiffUtils in Recycler view to update array ...
How to properly use DiffUtils in Recycler view to update array list? ... the same time when i use notifyDataSetChanged() app did not...
Read more >
DiffUtil vs. notifyDataSetChanged() - Educative.io
DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the...
Read more >
Efficient lists with DiffUtil and ListAdapter - Big Nerd Ranch
DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first ......
Read more >
Get Threading Right with DiffUtil - Jon F Hancock
Use DiffUtil for rich animations in RecyclerView with a tiny amount of code. Now this is a story all about how. My dataset...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found