Android : RecyclerView should not have too many items?

on Thursday, January 29, 2015


Please do not question the 'why' of this issue. I ran into it while prototyping some stuff, so will probably never end up in a released version. But that the crash occurred, troubled me.


I use RecyclerView with a horizontal oriented LinearLayoutManager.


I wanted to create a huge adapter, unlimited amount of items. So what I did was:



@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}


and to reuse the items:



@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {

position = position % (items.size() - 1);
...
// set the data
...


I have multiple RecyclerView in the layout btw, just FYI.


What happens next is, that the app hangs on startup, and finally throws this stacktrace:



E/AndroidRuntime( 3296): java.lang.StackOverflowError
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:411)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
E/AndroidRuntime( 3296): at android.support.v7.widget.ChildHelper$Bucket.insert(ChildHelper.java:401)
...


It should not mather how many items you have in the adapter, but some how, this is too many. Is there some prework being done for each item?


0 comments:

Post a Comment