How to use RecyclerView inside NestedScrollView

寵の児 提交于 2019-12-04 03:33:01

问题


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>

回答1:


just enable nestedscrolling that will enable smooth scrollview.

RecyclerView v = (RecyclerView) findViewById(...);
v.setNestedScrollingEnabled(false);



回答2:


Use this in your recyclerView

app:layout_behavior="@string/appbar_scrolling_view_behavior"



回答3:


<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...




回答4:


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>




回答5:


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




回答6:


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);



回答7:


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




回答8:


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);


来源:https://stackoverflow.com/questions/44453960/how-to-use-recyclerview-inside-nestedscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!