[英]How to use RecyclerView inside NestedScrollView
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. 我可以平滑地滚动RecyclerView
而无需滚动父视图。
Now I have to put the RecyclerView
inside a FrameLayout
like this: 现在我必须将RecyclerView
放在FrameLayout
如下所示:
<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. 现在我无法顺利滚动RecyclerView
。 All I want is: scroll RecyclerView
smoothly without scrolling parent view. 我想要的是:平滑滚动RecyclerView
而不滚动父视图。 What should I do? 我该怎么办?
EDIT : Here is my review_empty layout: 编辑 :这是我的review_empty布局:
<?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>
just enable nestedscrolling that will enable smooth scrollview. 只需启用nestedscrolling即可实现平滑的scrollview。
RecyclerView v = (RecyclerView) findViewById(...);
v.setNestedScrollingEnabled(false);
在recyclerView中使用它
app:layout_behavior="@string/appbar_scrolling_view_behavior"
<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... 试试这个...
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. 当您获得空数据时,只需将Recycler View Visibility设置为Gone,并将Empty View Visibility设置为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>
That may be a chance you loading images(static) in to ImageView can be slow your recyclerView. 这可能是您将图像(静态)加载到ImageView的可能性,这可能会使您的recyclerView变慢。
Or Big size of image. 或大尺寸的图像。
Try to run application on mobile have good size of RAM 尝试在移动设备上运行应用程序具有良好的RAM大小
Add fillViewport is true in NestedScrollView in xml file as below: 在xml文件的NestedScrollView中添加fillViewport为true,如下所示:
<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 并在您的活动setNested中滚动false,如下面的代码
recycler_view.setNestedScrollingEnabled(false);
只需将“不可滚动”布局放在嵌套滚动视图之外
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; 如果你想兼容API> 21,那么就使用;
recycler_view.setNestedScrollingEnabled(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.