android: RecyclerView inside a ScrollView
Solution 1
Set this property for the ScrollView,
android:fillViewport="true"
ScrollView will extend itself to fill the contents
Solution 2
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
totrue
- Or keep
RecyclerView
outside ofScrollView
. I my opinion, this is the best option by far. IfRecyclerView
height is not limited - which is the case when it's put intoScrollView
- 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 ofRecyclerView
.
Solution 3
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.
![nomongo](https://i.stack.imgur.com/ob2nl.png?s=256&g=1)
nomongo
Updated on June 28, 2020Comments
-
nomongo about 4 years
I have a
RecyclerView
wrapped in aLinearLayout
and it works perfectly as expected. I can see all the data in theRecyclerView
as populated. So far so good.When I wrap the
LinearLayout
in aScrollView
, theRecyclerView
goes blank. I do not see anything insideRecyclerView
. 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 aScrollView
.Thanks for all the help.
-
Jemshit Iskenderov about 9 years@beworker i have 2
RecyclerView
s and even i give certain heights, they won't be all visible when in landscape, soRecyclerView
insideScrollView
becomes necessary -
SilentKnight almost 9 yearsIt worked for me, but sometimes NullPointerException occurred.
-
Sathesh almost 9 yearsI would appreciate if you can provide a stack trace.
-
Jemshit Iskenderov almost 9 yearsDon't confuse with
fitsSystemWindows="true"
guys -
dasar almost 9 yearsPerfect point. So recyclerView inside ScrollView can be replaced with some static view container.
-
Khawar Raza over 7 yearsI had a similar problem. I was using Recyclerview inside Recyclerview items. The inner Recyclerview was not scrolling at all. Your solution worked for me.