android: RecyclerView inside a ScrollView
Asked Answered
W

4

14

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.

Wiggins answered 26/8, 2014 at 22:4 Comment(3)
Did you ever find an answer?Eugenides
try out: #27083591Rectilinear
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.Timaru
R
36

Set this property for the ScrollView,

 android:fillViewport="true"

ScrollView will extend itself to fill the contents

Rousing answered 27/1, 2015 at 23:34 Comment(3)
It worked for me, but sometimes NullPointerException occurred.Anking
I would appreciate if you can provide a stack trace.Rousing
Don't confuse with fitsSystemWindows="true" guysSnath
L
23

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.
Locomotor answered 14/9, 2014 at 7:44 Comment(2)
@beworker i have 2 RecyclerViews and even i give certain heights, they won't be all visible when in landscape, so RecyclerView inside ScrollView becomes necessarySnath
Perfect point. So recyclerView inside ScrollView can be replaced with some static view container.Emmanuel
C
7

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.

Cephalonia answered 28/4, 2016 at 17:1 Comment(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.Southeastwardly
A
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 .

Aruba answered 21/12, 2016 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.