RecyclerView应该不应该有太多物品? [英] RecyclerView should not have too many items?

查看:126
本文介绍了RecyclerView应该不应该有太多物品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请勿质疑此问题的原因".我在制作一些原型原型时遇到了它,因此可能永远不会发布到发行版中.但是那起车祸发生了,困扰着我.

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.

我将 RecyclerView 与水平放置的 LinearLayoutManager 一起使用.

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;
}

并重复使用这些物品:

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

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

在布局中我有多个 RecyclerView ,只是供参考.

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?

推荐答案

我遇到了同样的问题.我只在KitKat上转载了它.我的视图包含带有大型适配器(Integer.MAX_VALUE)的 RecyclerView ,以实现某种无限滚动.在KitKat上,我的 StackOverflowError 具有相同的堆栈跟踪.我注意到只有当我的 RecyclerView layout_weight = 0 FrameLayout 包装时,才会发生此错误.因此,我只是重写了视图以摆脱此 FrameLayout ,现在一切正常.我不知道它发生的确切原因以及为什么仅在Kitkat上发生,但我认为这与视图尺寸的测量有关.希望它会有所帮助.

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.

这篇关于RecyclerView应该不应该有太多物品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆