35

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.

4
  • try to set android:fillViewport="true" in scrollview Commented Jun 28, 2016 at 13:50
  • @LucasPaolillo Well, it doesn't help. Commented Jun 28, 2016 at 14:08
  • with android:fillViewPort="true", change the linear layout height to match_parent Commented Jun 28, 2016 at 14:12
  • 1
    @LucasPaolillo Still useless. Commented Jun 28, 2016 at 15:43

5 Answers 5

113

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.
Sign up to request clarification or add additional context in comments.

4 Comments

thanks. worked like a charm. I replaced ScrollView with NestedScrollView and everything is fine now.
helped me too, thanks, setNestedScrollingEnabled(false); is important to keep the smooth scrolling
Thats not a good solution actually. This has the effect that the RecyclerView doesnt recycle anymore.
@AhmetK Ok So what's the solution ?
6

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

Comments

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.............;)

Comments

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> 

Comments

0

try out this, it's work for me use fillViewPort="true" property of Scrollview and inside your recylerView put use it's property android:nestedScrollingEnabled="true".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.