android: RecyclerView inside a ScrollView
Set this property for the ScrollView,
android:fillViewport="true"
ScrollView will extend itself to fill the contents
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
.
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;}@Overridepublic void onTouchEvent(RecyclerView rv, MotionEvent e) {}@Overridepublic void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }});
I got this answer there. Thank you Piyush Gupta for that.