如何在Android中的嵌套滚动视图中使用RecyclerView

如何在Android中的嵌套滚动视图中使用RecyclerView,android,android-recyclerview,scrollbar,navigation-drawer,Android,Android Recyclerview,Scrollbar,Navigation Drawer,[更新]添加了样式和整个布局 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.

[更新]添加了样式和整个布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:id="@+id/navigation_view"
    android:layout_gravity="start"
    android:scrollbars="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cats_recycler_view"
        style="@style/catsRrecyclerView"
        android:background="@color/colorAccent"
        android:scrollbars="vertical"
        android:transcriptMode="alwaysScroll"
        android:nestedScrollingEnabled="true"/>

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ProgressBar
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/loading_indicator"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

    <TextView
        style="@style/notification_textView"
        android:id="@+id/notification_view"
        android:text="No Data Found" />

    <LinearLayout
        android:id="@+id/pages_layout"
        style="@style/pages_layout">

        <Button
            android:id="@+id/next_page"
            style="@style/prev_page.next_page"/>

        <TextView
            android:id="@+id/page_number"
            style="@style/page_number"
            android:text="Page 1"/>
        <Button
            android:id="@+id/prev_page"
            style="@style/prev_page" />



    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        style="@style/recyclerView" />

  </LinearLayout>

</android.support.v4.widget.DrawerLayout>
我在NavigationView中有一个RecyclerView,但它不会滚动显示整个项目列表。我尝试使用NestedScrollView,如回答中所述,但没有成功

<android.support.v4.widget.NestedScrollView
        android:layout_marginTop="30dp"
        android:id="@+id/herdchatHerdsNestedSV"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/cats_recycler_view"
            style="@style/catsRrecyclerView"
            android:background="@color/colorAccent"
            android:scrollbars="vertical"
            android:transcriptMode="alwaysScroll"
            android:nestedScrollingEnabled="true"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

但是这种方法不起作用,所以我现在正在尝试这种方法

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/navigation_view"
    android:layout_gravity="start"
    android:scrollbars="vertical">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/cats_recycler_view"
            style="@style/catsRrecyclerView"
            android:background="@color/colorAccent"
            android:scrollbars="vertical"
            android:transcriptMode="alwaysScroll"
            android:nestedScrollingEnabled="true"/>

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

风格


@id/页面\u布局
匹配父项
包装内容
垂直的
整体布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:id="@+id/navigation_view"
    android:layout_gravity="start"
    android:scrollbars="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cats_recycler_view"
        style="@style/catsRrecyclerView"
        android:background="@color/colorAccent"
        android:scrollbars="vertical"
        android:transcriptMode="alwaysScroll"
        android:nestedScrollingEnabled="true"/>

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ProgressBar
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/loading_indicator"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

    <TextView
        style="@style/notification_textView"
        android:id="@+id/notification_view"
        android:text="No Data Found" />

    <LinearLayout
        android:id="@+id/pages_layout"
        style="@style/pages_layout">

        <Button
            android:id="@+id/next_page"
            style="@style/prev_page.next_page"/>

        <TextView
            android:id="@+id/page_number"
            style="@style/page_number"
            android:text="Page 1"/>
        <Button
            android:id="@+id/prev_page"
            style="@style/prev_page" />



    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        style="@style/recyclerView" />

  </LinearLayout>

</android.support.v4.widget.DrawerLayout>


垂直滚动条可见,但不滚动。这里有什么问题以及如何解决?

尝试将RecyclerView的直接父级的高度更改为
wrap\u content
:)

在第一个示例中:

<android.support.v4.widget.NestedScrollView
    android:layout_marginTop="30dp"
    android:id="@+id/herdchatHerdsNestedSV"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The height of this one -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/cats_recycler_view"
            style="@style/catsRrecyclerView"
            android:background="@color/colorAccent"
            android:scrollbars="vertical"
            android:transcriptMode="alwaysScroll"
            android:nestedScrollingEnabled="true"/>

    </LinearLayout>

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

或者在第二个示例中(请注意,当列表小于屏幕大小时,这可能会导致不可预测的结果,因为根视图的高度与父视图不匹配):



查看是否有帮助?

问题是将NavigationView放置在根目录下的布局顶部。在其他视图之后将位置更改为布局的末尾,这样可以正常工作。

可能适配器布局根容器的
高度
设置为
匹配父级
?哪个元素高度应为换行内容?适配器中使用的布局根容器的
高度应设置为
换行内容
可以吗post
@style/catsRrecyclerView
请?添加了样式请检查我尝试了导航视图,但仍然无法工作。第一个示例有一个问题,因为它没有在抽屉中显示内容,但在主布局中不知道为什么。是否与主布局中的另一个recyclerview有冲突,或者只是高度问题?它现在是wrap_内容,仍然没有滚动。请提供一个在DroperView中的RecyclerView的工作示例?
<!-- The height of this one -->
<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:id="@+id/navigation_view"
    android:layout_gravity="start"
    android:scrollbars="vertical">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/cats_recycler_view"
            style="@style/catsRrecyclerView"
            android:background="@color/colorAccent"
            android:scrollbars="vertical"
            android:transcriptMode="alwaysScroll"
            android:nestedScrollingEnabled="true"/>

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