How to use RecyclerView
inside NestedScrollView
?
RecyclerView
content is not visible after setting adapter.
UPDATE layout code updated.
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/keyline_1">
</RelativeLayout>
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e5e5e5" />
<android.support.v7.widget.RecyclerView
android:id="@+id/conversation"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
If you are using
RecyclerView-23.2.1
or later. Following solution will work just fine:In your layout add RecyclerView like this:
And in your java file:
Here
layoutManager.setAutoMeasureEnabled(true);
will do the trick.Check out this issue and this developer blog for more information.
UPDATE 1
Since Android Support Library 23.2.0 there were added method
setAutoMeasureEnabled(true)
for LayoutManagers. It makes RecyclerView to wrap it's content and works like a charm.http://android-developers.blogspot.ru/2016/02/android-support-library-232.html
So just add something like this:
UPDATE 2
Since 27.1.0
setAutoMeasureEnabled
is deprecated, so you should provide custom implementation of LayoutManager with overridden methodisAutoMeasureEnabled()
But after many cases of usage RecyclerView I strongly recommend not to use it in wrapping mode, cause this is not what it is intended for. Try to refactor whole your layout using normal single RecyclerView with several items' types. Or use approach with LinearLayout that I described below as last resort
Old answer (not recommended)
You can use
RecyclerView
insideNestedScrollView
. First of all you should implement your own customLinearLayoutManager
, it makes yourRecyclerView
to wrap its content. For example:After that use this
LayoutManager
for yourRecyclerView
But you also should call those two methods:
Here
setNestedScrollingEnabled(false)
disable scrolling forRecyclerView
, so it doesn't intercept scrolling event fromNestedScrollView
. AndsetHasFixedSize(false)
determine that changes in adapter content can change the size of theRecyclerView
Important note: This solution is little buggy in some cases and has problems with perfomance, so if you have a lot of items in your
RecyclerView
I'd recommend to use customLinearLayout
-based implementation of list view, create analogue of Adapter for it and make it behave likeListView
orRecyclerView
Replace your recyclerView with,
here,
will manage the rest of things.
One more thing, no need to put your recyclerView inside NestedScrollView
I used RecyclerView inside a NestedScrollView and it worked for me. The only gotcha I had to keep in mind was that a NestedScrollView takes only one child view. So in my case I used of LienearLayout viewgroup which was housing my RecyclerView plus a number of other views that I needed.
I experience one issue putting my RecyclerView inside the NestedScrollView. I realized that scrolling the content of my RecyclerView slacked.
I later realized that my RecyclerView was receiving the scrolling event and therefore was conflicting with the scrolling behavior of the NestedScrollView.
So to solve that problem, I had to disable the scroll functionality of my RecyclerView with this method
movieListNewRecyclerView.setNestedScrollingEnabled(false);
You can checkout my Instagram for a short video of what I actually did. This is my instagram handle ofelix03
Click this image to see what I did