NestesScrollView中的RecyclerViews

时间:2017-01-29 11:06:01

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

我想在NestedScrollView中放置两个RecyclerViews,其中应显示每个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中,该片段包含在托管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>

两个RecyclerViews都使用垂直的LinearLayoutManager。

在RecyclerViews高度上使用match_parent并不会改变任何内容。

AppCompat版本:25.1.0

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

1 个答案:

答案 0 :(得分:0)

发现问题:

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