Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView inside ScrollView, some items are not shown

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.

like image 666
ywwynm Avatar asked Jun 28 '16 11:06

ywwynm


2 Answers

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.
like image 180
ywwynm Avatar answered Nov 20 '22 17:11

ywwynm


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

like image 2
Khalil Abubaker Avatar answered Nov 20 '22 17:11

Khalil Abubaker