13

I have a RecyclerView wrapped in a LinearLayout and it works perfectly as expected. I can see all the data in the RecyclerView as populated. So far so good.

When I wrap the LinearLayout in a ScrollView, the RecyclerView goes blank. I do not see anything inside RecyclerView. Why? How to make this work.

The page is one of the tabs in a ViewPagerIndicator, so everything in that tab needs to be in a ScrollView.

Thanks for all the help.

Vasily Kabunov
  • 5,179
  • 12
  • 41
  • 46
nomongo
  • 3,315
  • 7
  • 28
  • 33
  • 1
    Did you ever find an answer? – athor Sep 09 '14 at 21:06
  • try out: http://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working – Fabin Paul Sep 12 '15 at 04:16
  • you should have to use one child of scrollview and other views of your xml will be in that single child. for recyclerView's to work with scrollview, you have to give 0dp height to your recyclerview in xml and provide the max height that your recyclerview's can take at the runtime. You can calculate max height by multiplying the total number of rows * height of one row (in dp) in case of vertical recyclerView and in horizontal you can give height of one child only. Like if we have 10 textview of 40dp each, then vertical recyclerView height will be 10*40 dp = 400dp and 10dp for horizontal. – Vatish Sharma Feb 04 '16 at 10:39

4 Answers4

36

Set this property for the ScrollView,

 android:fillViewport="true"

ScrollView will extend itself to fill the contents

Sathesh
  • 5,846
  • 6
  • 33
  • 44
22

After checking implementation, the reason appears to be the following. If RecyclerView gets put into a ScrollView, then during measure step its height is unspecified (because ScrollView allows any height) and, as a result, gets equal to minimum height (as per implementation) which is apparently zero.

You have couple of options for fixing this:

  • Set a certain height to RecyclerView
  • Set ScrollView.fillViewport to true
  • Or keep RecyclerView outside of ScrollView. I my opinion, this is the best option by far. If RecyclerView height is not limited - which is the case when it's put into ScrollView - then all Adapter's views have enough place vertically and get created all at once. There is no view recycling anymore which kinda breaks the purpose of RecyclerView.
sergej shafarenka
  • 19,464
  • 6
  • 62
  • 82
  • @beworker i have 2 `RecyclerView`s and even i give certain heights, they won't be all visible when in landscape, so `RecyclerView` inside `ScrollView` becomes necessary – Jemshit Iskenderov Jun 11 '15 at 20:32
  • Perfect point. So recyclerView inside ScrollView can be replaced with some static view container. – dasar Sep 15 '15 at 11:28
6

Nothing helped me except this:

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        int action = e.getAction();
        switch (action) {
        case MotionEvent.ACTION_MOVE:
            rv.getParent().requestDisallowInterceptTouchEvent(true);
            break;
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
});

I got this answer there. Thank you Piyush Gupta for that.

Community
  • 1
  • 1
neustart47
  • 2,970
  • 4
  • 30
  • 57
  • 1
    I had a similar problem. I was using Recyclerview inside Recyclerview items. The inner Recyclerview was not scrolling at all. Your solution worked for me. – Khawar Raza Mar 20 '17 at 06:56
0

Hope this helps :

Add this line to your recyclerView xml :

android:nestedScrollingEnabled="false"

Try it ,recyclerview will be smoothly scrolled with flexible height inside scrollview .

iDeveloper
  • 1,420
  • 16
  • 43