5

I have this layout:

 <android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

            <--There is some layouts here-->
            <RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

It works well. I can scroll RecyclerView smoothly without scrolling parent view.

Now I have to put the RecyclerView inside a FrameLayout like this:

 <android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 <--There is some layouts here-->
        <FrameLayout
            android:id="@+id/review_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <include
                android:id="@+id/empty_view"
                layout="@layout/review_empty_view"
                android:visibility="gone" />
        </FrameLayout>
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

Now I cannot scroll the RecyclerView smoothly. All I want is: scroll RecyclerView smoothly without scrolling parent view. What should I do?

EDIT: Here is my review_empty layout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/shopping_review_empty" />

    <android.support.v4.widget.Space
        android:layout_width="32dp"
        android:layout_height="0dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/shopping_no_review_message" />
</LinearLayout>
TOP
  • 2,354
  • 5
  • 26
  • 55
  • add this line recyclerView.setNestedScrollingEnabled(false); – Akash Jun 09 '17 at 09:21
  • @Akash I added. It helps to scroll the `RecyclerView` smoothly. But it scrolls my parent view too. I don't want the parent view to be scrolled – TOP Jun 09 '17 at 09:23
  • What you want to achieve using FrameLayout? – Akash Jun 09 '17 at 09:24
  • I want to add a new view called "empty view" on top of `RecyclerView`. So I added both of them to a `FrameLayout` – TOP Jun 09 '17 at 09:25
  • @TOP can you explain me for what purpose u use FrameLayout? – ND1010_ Jun 09 '17 at 09:26
  • @ND1010_ I want to add a new view called "empty view" on top of `RecyclerView`. So I added both of them to a FrameLayout – TOP Jun 09 '17 at 09:26
  • @TOP did you mean by like any TextView or View? – ND1010_ Jun 09 '17 at 09:28
  • @ND1010_ You can see my second layout above. I included a "empty_view". It will be shown on top of `RecyclerView` in case there is no data – TOP Jun 09 '17 at 09:38
  • @TOP are load images in shopping_review_empty ImageView ? – ND1010_ Jun 09 '17 at 11:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146247/discussion-between-nd1010-and-top). – ND1010_ Jun 09 '17 at 11:05
  • Possible duplicate of [How to use RecyclerView inside NestedScrollView?](https://stackoverflow.com/questions/31000081/how-to-use-recyclerview-inside-nestedscrollview) – Mehdi Dehghani Apr 12 '18 at 08:47

8 Answers8

4

just enable nestedscrolling that will enable smooth scrollview.

RecyclerView v = (RecyclerView) findViewById(...);
v.setNestedScrollingEnabled(false);
jai khambhayta
  • 2,263
  • 12
  • 24
2

Use this in your recyclerView

app:layout_behavior="@string/appbar_scrolling_view_behavior"
1
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/review_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
       <ScrollView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_weight="1" >
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
        <RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
            </LinearLayout>
            </ScrollView>
        <include
            android:id="@+id/empty_view"
            layout="@layout/review_empty_view"
            android:visibility="gone" />
    </FrameLayout>
</LinearLayout>

</LinearLayout>

Try this...

Chandana_Raj
  • 110
  • 2
  • 12
1

I am suggesting you to do like this. When you get Empty Data then just set Recycler View Visibility as Gone and set Empty View Visibility as Visible.

Here is Code :

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:background="@color/bg"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_collapseMode="parallax">

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_30dp">

                <include layout="@layout/image_loading_progress" />

                <com.thenakedconvos.stories.utils.CircularImageView
                    android:id="@+id/ivProfile"
                    android:layout_width="@dimen/scale_90dp"
                    android:layout_height="@dimen/scale_90dp"
                    android:transitionName="@string/transition_image" />

            </FrameLayout>

            <com.thenakedconvos.stories.widget.CustomTextView
                android:id="@+id/tvUserName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_15dp"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:transitionName="@string/transition_text"
                app:typeface="gotham_medium" />

            <com.thenakedconvos.stories.widget.CustomTextView
                android:id="@+id/tvHoots"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_5dp"
                android:textColor="@color/text_color"
                android:textSize="12sp"
                app:typeface="gotham_book" />

            <com.vanniktech.emoji.EmojiTextView
                android:id="@+id/tvSmiley"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_10dp"
                android:textIsSelectable="true"
                app:emojiSize="25sp" />

            <com.thenakedconvos.stories.widget.CustomTextView
                android:id="@+id/tvAddFriend"
                android:layout_width="140dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_15dp"
                android:background="@drawable/red_rect_box"
                android:gravity="center"
                android:paddingBottom="@dimen/scale_12dp"
                android:paddingTop="@dimen/scale_12dp"
                android:text="Add"
                android:textColor="@color/red"
                android:textSize="14sp" />

        </LinearLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/AppTheme">

            <com.thenakedconvos.stories.widget.CustomTextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/text_color"
                android:textSize="18sp"
                app:typeface="bariol_bold" />
        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/scale_15dp"
            android:background="@color/white"
            android:orientation="vertical"
            android:padding="@dimen/scale_16dp">

            <com.thenakedconvos.stories.widget.CustomTextView
                android:id="@+id/tvUserStory"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Amy N. Johnson's Stories"
                android:textColor="@color/black"
                android:textSize="15sp"
                app:typeface="bariol_bold" />

            <com.thenakedconvos.stories.widget.CustomTextView
                android:id="@+id/tvTotalStories"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_5dp"
                android:text="110 Stories"
                android:textColor="@color/text_color"
                android:textSize="13sp"
                app:typeface="bariol_bold" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/scale_10dp"
                android:clipToPadding="false"
                android:paddingTop="@dimen/scale_10dp" />

        </LinearLayout>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

Akash
  • 913
  • 6
  • 14
  • I have to use `NestedScrollView`. Because in Landscape screen, there is not enough space for the `RecyclerView`. There are some other layout before `RecyclerView` (I do not show them in above code). So I need the `NestedScrollView` to allow users to scroll screen to see full `RecyclerView` – TOP Jun 09 '17 at 09:33
  • Yes you can do this using NestedScrollView too.Just Add this line if you are using NestedScrollView. recyclerView.setNestedScrollingEnabled(false); – Akash Jun 09 '17 at 09:34
  • I added. It helps to scroll the `RecyclerView` smoothly. But it scrolls my parent view too. I don't want to scroll the parent view while scrolling `RecyclerView` – TOP Jun 09 '17 at 09:41
  • No. Parent view means the whole `LinearLayout` inside the `NestedScrollView`. It is the view with id = "content" in above layout – TOP Jun 09 '17 at 09:45
  • Inside any View in NestedScroll is Scrolling. have you put any Image to your Linear Layout? – Akash Jun 09 '17 at 09:47
  • Please see my updated questions. I added some comments in the layout code. You can see: there are some layouts before the `FrameLayout` – TOP Jun 09 '17 at 09:51
  • so you don't want that commented layout will be scroll? – Akash Jun 09 '17 at 09:52
  • Yes. I only want to scroll my `RecyclerView` smoothly without scrolling other views. – TOP Jun 09 '17 at 09:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146240/discussion-between-akash-and-top). – Akash Jun 09 '17 at 09:55
1

That may be a chance you loading images(static) in to ImageView can be slow your recyclerView.

Or Big size of image.

Try to run application on mobile have good size of RAM

ND1010_
  • 2,848
  • 16
  • 33
0

Add fillViewport is true in NestedScrollView in xml file as below:

<android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:scrollbars="none">

And In your activity setNested scrolling false as below code

recycler_view.setNestedScrollingEnabled(false);
Patrick R
  • 5,704
  • 1
  • 18
  • 25
0

Just put your "not scrollable" layout outside the nested scroll view

mhdjazmati
  • 3,804
  • 1
  • 20
  • 32
0

All you have to do is use below line in your activity class:

ViewCompat.setNestedScrollingEnabled(recycler_view, false);

its compatible for lower versions also. and if you want to give compatible to API >21 only then use;

  recycler_view.setNestedScrollingEnabled(false);
Sagar Maiyad
  • 12,164
  • 8
  • 57
  • 96