Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put RecyclerView inside NestedScrollView?

With creation of NestedScrollView you can put scrolling view inside another scrolling view as long as those implement NestedScrollingChild and NestedScrollingParent correctly.

(This is not bad design pattern "Ian Lake (from Google) actually recommends putting a RecyclerView inside a nestedscrollview here: plus.google.com/u/0/+AndroidDevelopers/posts/9kZ3SsXdT2T")

I want to put RecyclerView inside NestedScrollView and fortunately RecyclerView implements NestedScrollingChild so you can put it inside NestedScrollView.

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild 

I have read these posts:

How to use RecyclerView inside NestedScrollView?

NestedScrolling with NestedScrollView, RecyclerView (Horizontal), inside a CoordinatorLayout

But the problem with most voted solution is, it calls all of the items of RecyclerView so for example if it is an endless RecyclerView and when the user reaches the end of the list you want to make a network request then with that solution the RecyclerView calls server repeatedly because it automatically reaches the last item of RecyclerView.

Anyway, how to set parameter so I can put RecyclerView inside NestedScrollView.(actually I want to put a viewgroup like framelayout or relativelayout as a single childe of nestedscrollview and then I want to put recyclerview inside framelayout or relativelayout)

When I put RecyclerView inside NestedScrollView there is nothing to display.


In order to create a sample project you can use cheesesquare and change the CheeseDetailActivity to have a RecyclerView.


Although the answer of BNK is not correct but BNK has tried a lot. So I award him the bounty. Still looking for nice solution....

like image 529
mmlooloo Avatar asked Oct 25 '15 13:10

mmlooloo


People also ask

What is nested scrolling in RecyclerView?

A android.view.ViewGroup that shows items in a horizontal scrolling list. NestedScrollView. NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android.

How do I make my recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .


1 Answers

The following is my new updated answer:

<android.support.v4.widget.NestedScrollView         android:id="@+id/scrollview"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:fillViewport="true"         app:layout_behavior="@string/appbar_scrolling_view_behavior">          <RelativeLayout             android:layout_width="match_parent"             android:layout_height="wrap_content">              <android.support.v7.widget.CardView                 android:id="@+id/cardview1"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:layout_margin="@dimen/card_margin">                  <LinearLayout                     style="@style/Widget.CardContent"                     android:layout_width="match_parent"                     android:layout_height="wrap_content">                      <TextView                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                         android:text="Info CardView1"                         android:textAppearance="@style/TextAppearance.AppCompat.Title" />                      <TextView                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                         android:text="@string/cheese_ipsum" />                  </LinearLayout>              </android.support.v7.widget.CardView>              <android.support.v7.widget.CardView                 android:id="@+id/cardview2"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:layout_below="@+id/cardview1"                 android:layout_margin="@dimen/card_margin">                  <LinearLayout                     style="@style/Widget.CardContent"                     android:layout_width="match_parent"                     android:layout_height="wrap_content">                      <TextView                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                         android:text="Info CardView2"                         android:textAppearance="@style/TextAppearance.AppCompat.Title" />                      <TextView                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                         android:text="@string/cheese_ipsum" />                  </LinearLayout>              </android.support.v7.widget.CardView>              <android.support.v7.widget.RecyclerView                 android:id="@+id/recyclerview"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:layout_below="@+id/cardview2"                 android:clipToPadding="false"                 android:paddingTop="0dp"/>          </RelativeLayout>      </android.support.v4.widget.NestedScrollView> 

In Activity:

        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(true); // true: with header         RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);                     final MyLinearLayoutManager layoutManager = new MyLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false, getScreenHeight(this));         // final CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);         recyclerView.setLayoutManager(layoutManager);         recyclerView.setAdapter(recyclerViewAdapter);           // recyclerView.setNestedScrollingEnabled(false); // Disables scrolling for RecyclerView, however, CustomLinearLayoutManager used instead of MyLinearLayoutManager 

I have also updated to My GitHub's sample project

Screenshot:

enter image description here


like image 172
BNK Avatar answered Sep 23 '22 00:09

BNK