【问题标题】:How to use RecyclerView inside NestedScrollView如何在 NestedScrollView 中使用 RecyclerView
【发布时间】:2017-06-09 09:18:12
【问题描述】:

我有这个布局:

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

效果很好。我可以在不滚动父视图的情况下平滑滚动RecyclerView

现在我必须将 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>

现在我无法顺利滚动RecyclerView。 我想要的是:平滑滚动RecyclerView 而不滚动父视图。我该怎么办?

编辑:这是我的 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>

【问题讨论】:

  • 添加这一行 recyclerView.setNestedScrollingEnabled(false);
  • @Akash 我添加了。它有助于平滑滚动RecyclerView。但它也滚动了我的父视图。我不想滚动父视图
  • 你想用FrameLayout实现什么?
  • 我想在RecyclerView 之上添加一个名为“空视图”的新视图。所以我将它们都添加到FrameLayout
  • @TOP 你能解释一下你使用 FrameLayout 的目的吗?

标签: android android-recyclerview android-nestedscrollview


【解决方案1】:

只需启用将启用平滑滚动视图的嵌套滚动。

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

【讨论】:

  • 有助于平滑滚动 RecyclerView。但它也会滚动其他视图。我不想滚动其他视图,而我只滚动 RecyclerView
  • android:fillViewport="true" 将此添加到您的nestedschroll
  • 我加了但是还是一样
【解决方案2】:

在你的 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>
    

    试试这个...

    【讨论】:

      【解决方案4】:

      我建议你这样做。当您获得 Empty Data 时,只需将 Recycler View Visibility 设置为 Gone 并将 Empty View Visibility 设置为 Visible。

      这里是代码:

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

      【讨论】:

      • 我必须使用NestedScrollView。因为在横向屏幕中,RecyclerView 没有足够的空间。 RecyclerView 之前还有一些其他的布局(我没有在上面的代码中显示它们)。所以我需要NestedScrollView 让用户滚动屏幕以查看完整的RecyclerView
      • 是的,您也可以使用 NestedScrollView 执行此操作。如果您使用的是 NestedScrollView,只需添加此行。 recyclerView.setNestedScrollingEnabled(false);
      • 我添加了。它有助于平滑滚动RecyclerView。但它也滚动了我的父视图。我不想在滚动时滚动父视图RecyclerView
      • 没有。父视图表示NestedScrollView 内的整个LinearLayout。它是上面布局中 id = "content" 的视图
      • 在 NestedScroll 中的任何视图内部都是滚动的。你有没有把任何图像放到你的线性布局中?
      【解决方案5】:

      这可能是您将图像(静态)加载到 ImageView 的机会可能会减慢您的 recyclerView。

      或图片尺寸大。

      尝试在具有良好 RAM 大小的移动设备上运行应用程序

      【讨论】:

        【解决方案6】:

        在 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">
        

        并且在您的活动中 setNested 滚动 false 如下代码

        recycler_view.setNestedScrollingEnabled(false);
        

        【讨论】:

        • 当我滚动 RecyclerView 时,我不希望其他视图也被滚动。您的代码帮助我顺利滚动 RecyclerView。但它也会滚动整个视图
        • 请提供“review_empty_view”的代码,以便我进一步提供帮助
        • 查看我更新的问题。我添加了“review_empty_view”
        • 请将您的 recyclerView 高度设置为 100 或 200dp,让我知道它是否适合您
        • 不要这样做。它将防止视图被回收。
        【解决方案7】:

        只需将您的“不可滚动”布局放在嵌套滚动视图之外

        【讨论】:

          【解决方案8】:

          您所要做的就是在您的活动类中使用以下行:

          ViewCompat.setNestedScrollingEnabled(recycler_view, false);
          

          它也兼容低版本。如果您只想兼容 API >21,请使用;

            recycler_view.setNestedScrollingEnabled(false);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-05-22
            • 1970-01-01
            • 2016-01-24
            • 1970-01-01
            • 2018-12-12
            相关资源
            最近更新 更多