RecyclerView inside ScrollView, some items are not shown
S

4

34

I had a RecyclerView in ScrollView like this:

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

And the RecyclerView's item is a RelativeLayout, inside of which there is an EditText and other views. The layout_height of that RelativeLayout and EditText is both wrap_content. User can input into that EditText without any limit of length/lines so that each item's height is different.

Then I found that getItemCount() in Adapter returns true value but onBindViewHolder() is called of wrong times(less than it should be), thus not enough to show all items.

I found that this will happen only if I wrote recyclerView.setNestedScrollingEnabled(false). But I cannot remove this line. Because if I did so, the RecyclerView won't scroll smoothly and is not harmonious with other views inside ScrollView and ScrollView itself.

This occurs on 6.0 but not on 4.1.

I communicated with Google at this page: https://code.google.com/p/android/issues/detail?id=213914 and he told me this is a bug fix for RecyclerView. You can visit that page so that you can understand the question and my goal better(There is a small sample project to show the problem there). I don't agree with him even now and I want to solve the problem. Please help, thank you in advance.

Slippage answered 28/6, 2016 at 11:26 Comment(4)
try to set android:fillViewport="true" in scrollviewSchroth
@LucasPaolillo Well, it doesn't help.Slippage
with android:fillViewPort="true", change the linear layout height to match_parentSchroth
@LucasPaolillo Still useless.Slippage
S
112

I found the solution myself: replace ScrollView with NestedScrollView and keep recyclerView.setNestedScrollingEnabled(false). I don't know if this is what NestedScrollView is made for but it works.

NOTICE:

  1. NestedScrollView is not a child of ScrollView but of FrameLayout.
  2. This solution will also bring some bugs with self-simulated adjustResize.
Slippage answered 29/6, 2016 at 1:36 Comment(5)
thanks. worked like a charm. I replaced ScrollView with NestedScrollView and everything is fine now.Anopheles
helped me too, thanks, setNestedScrollingEnabled(false); is important to keep the smooth scrollingJack
Thats not a good solution actually. This has the effect that the RecyclerView doesnt recycle anymore.Opposable
Brilliant answer. Thanks:)Brotherton
@AhmetK Ok So what's the solution ?Hearne
F
5

In my case, I replaced LineaLayout with RelativeLayout and it's solved the issue and all items have shown.

Florance answered 6/4, 2022 at 18:30 Comment(1)
This solved my problem .. thank youMcdonald
C
2

The answer is:

androidx.core.widget.NestedScrollView

In the first step, you need to create NestedScrollView element in XML:

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

Next, add the below attribute to recyclerView:

android:overScrollMode="never"

Then, the recyclerView will be as following:

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:overScrollMode="never" />

Finally, the whole the layout will be something like below, you can add other materials inside 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.............;)

Canto answered 1/6, 2022 at 9:19 Comment(0)
A
0

The best solution is to keep multiple Views in a Single View / View Group and then keep that one view in the SrcollView. ie.

Format -

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

Eg.

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

Another Eg. of ScrollView with multiple Views

<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>
Acquaint answered 30/10, 2020 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.