【问题标题】:RecyclerViews inside NestesScrollViewNestedScrollView 内的 RecyclerView
【发布时间】:2017-01-29 11:06:00
【问题描述】:

我想在 NestedScrollView 中放置两个 RecyclerView,每个 RecyclerView 的所有项目都应该在其中显示:

Desired:                    Current:
---------------------       --------------------- 
Fixed Header content        Fixed Header content
---------------------       ---------------------
<NestedScrollView>          <NestedScrollView>
  EditText                    EditText 
  EditText                    EditText
  ...                         ...
  <RecyclerView>              <RecyclerView> (scrollable>
   Item 1                       Item 1
   Item 2                     </RecyclerView>
   ...                        <RecyclerView> (scrollable>
   Item N                       Item 2
  </RecyclerView>             </RecyclerView>
  <RecyclerView>              </NestedScrollView>
   Item 1                     --------------------
   Item 2
   ...
   Item M
  </RecyclerView>
</NestedScrollView>
----------------------

目前,每个 RecyclerView 仅显示第一项,您可以在此有限空间内向下滚动到其他项。

我尝试设置RecyclerView.setNestedScrollingEnabled(false);,其效果仍然只显示RecyclerView的第一项,并且RecyclerView不再可滚动。

这一切都包含在 Fragment 中,该 Fragment 包含在托管 BottomNavigationView 的 Activity 中。

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout     
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/coordinatorLayoutMain"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context=".gui.activity.MainActivity">

  <android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    app:elevation="0dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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


  <FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

 <android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/white"
    app:itemTextColor="@color/bottom_navigation_item_color"
    app:itemIconTint="@color/bottom_navigation_item_color"
    app:menu="@menu/menu_bottom_main_activity" />

</android.support.design.widget.CoordinatorLayout>

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    ... fixed header content ...

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="true"
            >

            <android.support.design.widget.TextInputLayout 
              ...
            />

            <TextView
                android:id="@+id/catHeader1"
                style="@style/Me.CatHeader"
                android:layout_below="@id/dummy1"
                android:text="@string/textCat1" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/catHeader1"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            <TextView
                android:id="@+id/action1"
                style="@style/Me.AddText"
                android:layout_below="@id/recyclerView1"
                android:text="@string/action1"
                android:visibility="gone" />

            <TextView
                android:id="@+id/catHeader2"
                style="@style/Me.CatHeader"
                android:layout_below="@id/action1"
                android:text="@string/textCat2" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/catHeader2"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

              <TextView
                android:id="@+id/action2"
                style="@style/Me.AddText"
                android:layout_below="@id/recyclerView2"
                android:text="@string/action2"
                android:visibility="gone" />

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

两个 RecyclerView 都使用垂直 LinearLayoutManager。

在 RecyclerViews 高度上使用 match_parent 不会改变任何东西。

AppCompat 版本:25.1.0

我错过了什么,我做错了什么?

【问题讨论】:

  • 很抱歉以这种方式与您联系(我的评论与您在此处的输入无关)。我遇到了一个你在“分类”中投票的问题,你做出了错误的选择。请:仔细研究分类帮助,避免将不属于那里的项目放入编辑队列。我希望您将此视为改善投票的机会。我特指stackoverflow.com/review/triage/21080728。如果您对我有其他问题或反馈,请随时给我留言。如果你给我一个快速的回复,我会立即在这里评论。

标签: android android-layout android-recyclerview android-nestedscrollview


【解决方案1】:

发现问题:

项目的根布局有layout_height = match_parent,将其设置为layout_height = ?listPreferredItemHeight 解决了所有问题。

【讨论】:

  • 你为什么使用 2 个 RecyclerView 的?
  • 两个 RecyclerViews 显示非常不同的内容/项目,将其放入两个适配器而不是一个处理更容易。
  • 无论您是否具有相同的数据类型,创建单独的 RecyclerView 或 ListView 都不是一个好习惯。
猜你喜欢
  • 1970-01-01
  • 2017-02-15
  • 2017-05-28
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 2019-04-26
  • 2019-06-14
  • 2020-06-01
相关资源
最近更新 更多