当在LinearLayout中使用权重时,Recyclerview onBindViewHolder会调用所有项 [英] Recyclerview onBindViewHolder called for all items when inside LinearLayout with weights

查看:129
本文介绍了当在LinearLayout中使用权重时,Recyclerview onBindViewHolder会调用所有项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从支持库recyclerview-v7:23.1.1更新到recyclerview-v7:25.1.0.

I recently updated from support library recyclerview-v7:23.1.1 to recyclerview-v7:25.1.0.

我的布局"包含2个Recylerviews,它们在屏幕上的比例为50%. xml代码如下:

My Layout contains 2 recylerviews splitted 50% on the screen. The xml code is as follows:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scrollbars="none"/>

    <android.support.v4.widget.Space
        android:layout_width="@dimen/two_dp"
        android:layout_height="match_parent"
        android:background="@color/dark_gray"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scrollbars="none"/>

</LinearLayout>

现在,将为所有列表项而不是仅可见项调用onBindViewHolder. 在更新为支持库25.1.0后开始发生这种情况.

Now onBindViewHolder is called for all list items instead of only the visible items. This started happening after update to support library 25.1.0.

如果除去砝码,效果很好,但是需要并排放置2个Recylcerviews.

It is working fine if weights are removed, but having 2 recylcerviews side by side is required.

如何告诉recyclerview回收视图而不是全部加载?

更新:在棉花糖及以上设备上运行正常.该问题存在于Lollipop或更低版本中. 您可以在此处找到演示项目: https://bitbucket.org/aniketit/recyclerviewtest

UPDATE: It works fine on Marshmallow and above devices. The issue is present in Lollipop or below. You can find a demo project here: https://bitbucket.org/aniketit/recyclerviewtest

推荐答案

我遇到了相同的问题,该问题在删除了weighted和0dp元素后仍然存在.就我而言,这个问题相当琐碎-我无意间将RecyclerView放入了带有fillViewPort = true的NestedScrollView内.这将导致适配器将构建视图的所有元素,并且您会注意到响应时间显着延迟.

I ran into the same issue which persisted after removing weighted and 0dp elements. In my case, the issue was rather trivial - I had inadvertently put my RecyclerView inside of a NestedScrollView with fillViewPort=true. This causes the Adapter will build all the elements for the view and you will notice a significant delay in responsiveness.

事实证明,如果您也只在普通的旧ScrollView中安装了RecyclerView,就会出现此问题.

Turns out the issue will occur if you just have the RecyclerView in a plain old ScrollView too.

这篇关于当在LinearLayout中使用权重时,Recyclerview onBindViewHolder会调用所有项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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