【问题标题】:NestedScrollView and RecyclerView no scroll even though they're not nested?NestedScrollView 和 RecyclerView 没有滚动,即使它们没有嵌套?
【发布时间】:2019-08-02 13:32:25
【问题描述】:

我遇到了一个以前从未遇到过的非常奇怪的问题。

所以,我有一个视图 (BottomSheet),它封装了 RecyclerView 及其空视图。我将其中一个设置为View.GONE,另一个设置为View.VISIBLE,具体取决于是否有要显示的项目。 空视图实际上包含在 NestedScrollView 中,因为我需要能够滚动它以便用户能够向上或向下移动 BottomSheet。

问题是,取决于我如何构建视图,滚动实际上是在RecyclerViewNestedScrollView 上,无论我放在哪个前面,而不是两者。他们的父母是RelativeLayout(看看下面的代码)。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/BottomSheet"
    >

    <androidx.core.widget.NestedScrollView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        tools:visibility="visible"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:layout_marginTop="115dp"
            android:orientation="vertical"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginTop="7dp"
                android:textAlignment="center"
                />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

    <com.myproject.CustomRecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="36dp"
        android:paddingTop="10dp"
        app:emptyView="@id/empty_view"
        />

</RelativeLayout>

【问题讨论】:

  • android:layout_below="@id/header" 标题在哪里? android:visibility="gone" tools:visibility="visible"你为什么有这两个
  • 我相应地编辑了问题,视图实际上有点复杂,但这是问题的核心(如果我准确地说,我会遇到同样的问题)。我有android:visibility="gone" 来隐藏启动时的视图,但tools:visibility="visible" 因为它让我看到空视图在设计方面是否可以

标签: android android-recyclerview


【解决方案1】:

1.- 在您的 NestedScrollView 中放入:

android:overScrollMode="never"

现在当我们使用nestedScrollView 并将recyclerView 放入nestedScrollView 时发生了什么样的问题,它会根据手势以不同的速度滚动。滚动功能将不流畅。

2.-因此,要解决此问题,您只需在设置适配器后即可 把这一行:

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

现在你的 recyclerview 可以平滑滚动了……

希望对你有帮助!

【讨论】:

  • 尝试使用 coordynaitorlayout
  • 是的,我也试过了,我也试过 LinearLayout 没有运气
  • 但是在嵌套中添加你的回收器
  • 这很危险,因为如果你将recyclerview 包裹在一个nestedscrollview 中,那么recyclerview 将一次性绑定所有的项目,因为nestedscrollview 需要子视图的全高才能工作。这就是我将它们分开的原因,以及为什么你永远不应该将 recyclerview 作为nestedscrollview 的孩子
  • 我给出这个答案是因为你需要它。我知道这个问题。这就是为什么我说2个步骤。你也可以在你的回收站上使用 android:nestedScrollingEnabled="false"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
相关资源
最近更新 更多