【问题标题】:RecyclerView inside ScrollView, some items are not shownScrollView里面的RecyclerView,有些item没有显示
【发布时间】:2016-10-30 16:57:28
【问题描述】:

我在 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 的错误修复。您可以访问该页面,以便更好地理解问题和我的目标(有一个小示例项目可以在那里显示问题)。我现在也不同意他的观点,我想解决这个问题。请帮忙,提前谢谢。

【问题讨论】:

  • 尝试在滚动视图中设置android:fillViewport="true"
  • @LucasPaolillo 好吧,它没有帮助。
  • 用android:fillViewPort="true",将线性布局高度改为match_parent
  • @LucasPaolillo 还是没用。

标签: android android-recyclerview android-support-library android-6.0-marshmallow android-scrollview


【解决方案1】:

我自己找到了解决方案:将ScrollView 替换为NestedScrollView 并保留recyclerView.setNestedScrollingEnabled(false)。我不知道这是否是 NestedScrollView 的用途,但它确实有效。

注意:

  1. NestedScrollView 不是ScrollView 的孩子,而是FrameLayout 的孩子。
  2. 这个方案也会带来一些自模拟adjustResize的bug。

【讨论】:

  • 谢谢。像魅力一样工作。我用 NestedScrollView 替换了 ScrollView,现在一切正常。
  • 也帮助了我,谢谢,setNestedScrollingEnabled(false); 对保持平滑滚动很重要
  • 这实际上不是一个好的解决方案。这具有 RecyclerView 不再回收的效果。
  • 出色的答案。谢谢:)
【解决方案2】:

最好的解决方案是将multiple Views 保留在Single View / View Group 中,然后将那个视图保留在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>

另一个例子。具有多个 View 的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-04
    • 2018-09-30
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2017-01-09
    相关资源
    最近更新 更多