RecyclerView should not have too many items?

只愿长相守 提交于 2019-12-11 02:53:42

问题


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?


回答1:


I had same problem. I reproduced it only on KitKat. My view contains RecyclerView with huge adapter (Integer.MAX_VALUE) to implement some kind of infinite scrolling. On KitKat I had StackOverflowError with same stacktrace. I noticed that this error happened only if my RecyclerView was wrapped with FrameLayout with layout_weight=0. So I just rewrote my view to get rid of this FrameLayout and everything works ok now. I don't know exactly reason why it happened and why it happened only on Kitkat, but I think it related to view sizes measuring. Hope it will help.



来源:https://stackoverflow.com/questions/28210665/recyclerview-should-not-have-too-many-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!