【问题标题】:How to use RecyclerView inside a nested scroll view in Android如何在 Android 的嵌套滚动视图中使用 RecyclerView
【发布时间】:2019-05-07 22:26:46
【问题描述】:

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

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

风格

<style name="catsRrecyclerView">
    <item name="android:layout_below">@id/pages_layout</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:scrollbars">vertical</item>
</style>

整体布局

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

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

【问题讨论】:

  • 也许您的适配器布局根容器已将height 设置为match_parent
  • 哪个元素高度应该是wrap_content?
  • 适配器中使用的布局的根容器应将height 设置为wrap_content
  • 你能发帖@style/catsRrecyclerView吗?
  • 样式添加请检查

标签: android android-recyclerview scrollbar navigation-drawer


【解决方案1】:

问题在于将 NavigationView 放置在布局的顶部,就在根的下方。在其他视图使其正常工作后将位置更改为布局的末尾。

【讨论】:

    【解决方案2】:

    尝试将 RecyclerView 的直接父级的高度更改为 wrap_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>
    

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

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

    看看有没有用?

    【讨论】:

    • 我试过 NavigationView 但还是不行。第一个示例有一个问题,因为它没有在抽屉中显示内容,但在主布局中不知道为什么。与主布局中的另一个recyclerview有冲突还是只是高度问题?它现在是 wrap_content 并且仍然没有滚动
    • 您能否提供一个在 DrawerView 中的 RecyclerView 的工作示例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2019-03-23
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多