How to fix 'Design assumption violated' error in ViewPager2?

I'm using androidx.viewpager2:viewpager2:1.0.0

For whoever still stuck at this, and need to implement getItemId() containsItem(). Please chek your getItemId() and containsItem() implementation. My problem is when I serve 10 item with possibility several item have same value, this error occurs.

Media have several data with same value.

private var mediaId = media.map { it.hashCode().toLong() }

override fun getItemId(position: Int): Long = mediaId[position]

override fun containsItem(itemId: Long): Boolean = mediaId.contains(itemId)

while you updating the data or even simply swiping, the viewpager is confuse because you implement getItemId() with non unique id. The solution is just make sure your data is unique or have their own id. *My media datas dont have id.

If you open the implementation of getItemId() notes this part :

  • @return stable item id {@link RecyclerView.Adapter#hasStableIds()}

you need to have unique id for each of item.


I had this same problem with ViewPager2 on configuration change. I'm using:

implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta03'

In my case, I had overridden the getItemId() method in my FragmentStateAdapter class. When I got rid of my getItemId() method, the "IllegalStateException: Design assumption violated" error was no more! :-)