ScrollView内部的RecyclerView,某些项目未显示

时间:2016-06-28 11:26:07

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

我在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和其他视图。 layout_heightRelativeLayout的{​​{1}}都是EditText。用户可以输入wrap_content而没有任何长度/行限制,以便每个项目的高度不同。

然后我发现EditText中的getItemCount()会返回真值,但会调用Adapter错误的时间(少于它应该),因此不足以显示所有项目。

我发现只有在我写onBindViewHolder()时才会发生这种情况。但我不能删除这一行。因为如果我这样做了,recyclerView.setNestedScrollingEnabled(false)就会顺利滚动,与RecyclerViewScrollView内部的其他观点不一致。

这发生在6.0但不是4.1。

我在此页面与Google沟通:https://code.google.com/p/android/issues/detail?id=213914他告诉我这是ScrollView的错误修复程序。您可以访问该页面,以便更好地理解问题和我的目标(有一个小样本项目可以显示问题)。即使是现在我也不同意他,我想解决这个问题。请帮助,提前谢谢。

2 个答案:

答案 0 :(得分:62)

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

注意:

  1. NestedScrollView不是ScrollView的孩子,而是FrameLayout的孩子。
  2. 此解决方案还会带来一些自我模拟的错误adjustResize

答案 1 :(得分:0)

最好的解决方案是将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>

另一个。带有多个视图的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>
相关问题