ScrollView内的RecyclerView,有些项目没有显示。

33

我有一个在ScrollView中的RecyclerView,就像这样:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--other stuff-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"/>

    </LinearLayout>

    <!--other stuff-->

</ScrollView>

而且 RecyclerView 的子项是一个 RelativeLayout,里面有一个 EditText 和其他视图。那个 RelativeLayoutEditTextlayout_height 都是 wrap_content。用户可以在没有长度/行限制的情况下输入到那个 EditText 中,因此每个项目的高度都不同。

然后我发现 Adapter 中的 getItemCount() 返回正确的值,但 onBindViewHolder() 被调用的次数错误(少于应该调用的次数),因此不足以显示所有项目。

我发现这只会发生在我写了 recyclerView.setNestedScrollingEnabled(false)时。但我不能删除这行代码。因为如果这样做,RecyclerView 将无法平稳滚动,并且与 ScrollViewScrollView 内部的其他视图不协调。

这在 6.0 上发生,但在 4.1 上没有发生。

我在这个页面上与 Google 进行了沟通:https://code.google.com/p/android/issues/detail?id=213914,他告诉我这是修复 RecyclerView 的 bug。您可以访问该页面,以便更好地理解问题和我的目标(那里有一个小示例项目显示问题)。即使现在我也不同意他的看法,我想解决这个问题。请帮忙,谢谢。


尝试在ScrollView中设置android:fillViewport="true"。 - Lucas Ferraz
@LucasPaolillo 嗯,这并没有帮助。 - ywwynm
使用android:fillViewPort="true",将线性布局的高度更改为match_parent。 - Lucas Ferraz
1
@LucasPaolillo 仍然毫无用处。 - ywwynm
4个回答

111
我找到了解决方法:用NestedScrollView替换ScrollView并保持recyclerView.setNestedScrollingEnabled(false)不变。我不知道NestedScrollView是否是为此而设计的,但它确实起作用。
注意事项:
  1. NestedScrollView不是ScrollView的子类,而是FrameLayout的子类。
  2. 这种解决方法也会带来一些自模拟的adjustResize的错误。

谢谢。运行得很好。我用NestedScrollView替换了ScrollView,现在一切都正常了。 - Däñish Shärmà
2
也帮助了我,谢谢,setNestedScrollingEnabled(false);很重要,可以保持平滑的滚动。 - user2582318
12
实际上,那不是一个好的解决方案。这会导致RecyclerView不再进行回收利用。 - Ahmet K
绝妙的答案。谢谢:) - Gundu Bandgar
@AhmetK 好的,那么解决方案是什么? - Milad Faridnia

5
在我的情况下,我用RelativeLayout替换了LineaLayout,问题得到了解决,所有项目都显示出来了。

这解决了我的问题..谢谢 - undefined

2
答案是:
androidx.core.widget.NestedScrollView

在第一步中,您需要在XML中创建NestedScrollView元素:
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    // RecyclerViews should be located here

    </LinearLayout>
</androidx.core.widget.NestedScrollView>

接下来,将以下属性添加到recyclerView中:
android:overScrollMode="never"

接下来,recyclerView 将如下所示:
<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:overScrollMode="never" />

最后,整个布局将类似于下面这样,您可以在LinearLayout内添加其他材料:
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:overScrollMode="never" />
        // other materials
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

Celebrate.............;)


0

最佳解决方案是在单个视图/视图组中保留多个视图,然后将该视图放入SrcollView中。即。

格式 -

<ScrollView> 
  <Another View>
       <RecyclerView>
       <TextView>
       <And Other Views>
  </Another View>
</ScrollView>

例如。

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView           
              android:text="any text"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>


        <TextView           
              android:text="any text"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
 </ScrollView>

另一个具有多个视图的ScrollView示例

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            android:layout_weight="1">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingHorizontal="10dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/CategoryItem"
                    android:textSize="20sp"
                    android:textColor="#000000"
                    />

                <TextView
                    android:textColor="#000000"
                    android:text="₹1000"
                    android:textSize="18sp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:textColor="#000000"
                    android:text="so\nugh\nos\nghs\nrgh\n
                    sghs\noug\nhro\nghreo\nhgor\ngheroh\ngr\neoh\n
                    og\nhrf\ndhog\n
                    so\nugh\nos\nghs\nrgh\nsghs\noug\nhro\n
                    ghreo\nhgor\ngheroh\ngr\neoh\nog\nhrf\ndhog"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

             </LinearLayout>

        </LinearLayout>

</ScrollView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接