【问题标题】:Recyclerview with fixed height wont scroll inside nested scroll view具有固定高度的 Recyclerview 不会在 nestedscrollview 内滚动
【发布时间】:2019-05-29 20:59:12
【问题描述】:

我在NestedScrollView 中有一个固定高度的RecyclerView,其中还有一些其他布局。回收站视图不会滚动,但如果我将其高度设置为wrap_content,它会很好地滚动。

我无法让RecyclerView 使用wrap_content,因为EndlessRecyclerViewScrollListener 存在一个问题,即使用户根本没有向下滚动,它也会不断从服务器加载数据并将其推送到我的Adapter

大多数人建议将嵌套滚动设置为 false,但如果我禁用嵌套滚动,NestedScrollView 不允许我滚动我的RecyclerView。但是如果我启用嵌套滚动,除非我从RecyclerView 外部开始触摸,否则滚动视图不会滚动。

我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/scoop_background"
tools:context=".module.scoop.timeline.ScoopTimelineFragment">

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="92dp">

        <ImageView
            android:id="@+id/ivTimelineBanner"
            android:layout_width="match_parent"
            android:layout_height="92dp"
            android:layout_margin="0dp"
            android:clickable="true"
            android:padding="0dp"
            android:scaleType="fitXY"
            android:src="@drawable/banner_placeholder" />

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:paddingLeft="92dp">

                <TextView
                    android:id="@+id/tvGroupMembership"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/text_content" />

                <TextView
                    android:id="@+id/tvGroupName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/text_header"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tvGroupMemberCount"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/text_content" />

            </LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@null"
                android:paddingStart="@dimen/divider_normal"
                android:paddingEnd="@dimen/divider_normal"
                android:src="@drawable/ic_chevron_right_white_24dp" />

        </LinearLayout>

    </FrameLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayoutTimeline"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:overScrollMode="never"
            android:scrollbars="none">

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

                <android.support.v7.widget.CardView
                    android:id="@+id/cvCreateScoop"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/divider_small"
                    android:layout_marginRight="@dimen/divider_small"
                    android:clickable="true"
                    android:focusable="true"
                    android:foreground="?attr/selectableItemBackground"
                    app:cardCornerRadius="8dp"
                    app:cardUseCompatPadding="true">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:padding="@dimen/divider_normal">

                        <ImageView
                            android:layout_width="32dp"
                            android:layout_height="32dp"
                            android:src="@drawable/svg_nav_create_scoop" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_vertical"
                            android:paddingStart="@dimen/divider_normal"
                            android:text="What's on your mind?"
                            android:textSize="@dimen/text_content" />

                        <ImageView
                            android:layout_width="32dp"
                            android:layout_height="32dp"
                            android:padding="@dimen/divider_xsmall"
                            android:src="@drawable/svg_insert_image" />
                    </LinearLayout>

                </android.support.v7.widget.CardView>

                <RelativeLayout
                    android:id="@+id/relativeLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/rv"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clipToPadding="false"
                        android:overScrollMode="never"
                        android:nestedScrollingEnabled="false"
                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:paddingBottom="4dp">

                    </android.support.v7.widget.RecyclerView>


                </RelativeLayout>

            </LinearLayout>

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

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

</LinearLayout>

我需要修改什么以使RecyclerView 可滚动,但整个NestedScrollView 也随之滚动,因此如果用户向上滚动(从上到下滑动),@987654334 @ 将与 NestedScrollView 一起滚动,这会将布局带到 RecyclerView 上方。

可视化:

我试图实现的布局类似于 facebook 的布局。向下滚动时,时间线会向下滚动,顶部带有信使图标的搜索栏也会滚动,因此向下滚动时会隐藏。当您向上滚动时,时间线将向上滚动,并再次显示搜索栏。

【问题讨论】:

  • 在此处发布您的代码
  • 我编辑了答案以显示我的布局代码
  • 相对布局高度是固定的,我已经通过代码完成了。所以应该忽略 recycler 视图布局上的 match_parent。
  • 我们有同样的问题。你现在有解决方案吗?

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


【解决方案1】:

我决定用不同的方法来解决它,因为我无法用这种方法解决它。相反,我使用了 CollapsingToolbar 并将其他布局放入其中,然后移除其背景,使其看起来不像工具栏,并且它可以无缝地执行我想要的操作,只是使用不同的实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    相关资源
    最近更新 更多