NestedScrollView和RecyclerView即使没有嵌套也无法滚动?

4
我遇到了一个很奇怪的问题,以前从未经历过。
我有一个视图(BottomSheet),它封装了一个RecyclerView和它的空视图。我将它们之一设为View.GONE,另一种设为View.VISIBLE,具体取决于是否有要显示的项目。由于需要让用户能够将BottomSheet向上或向下移动,因此空视图实际上被包裹在NestedScrollView中。
问题是,根据我的视图结构如何组织,滚动实际上是在RecyclerView或NestedScrollView上进行的,无论我先放哪个,而不是两者兼具。它们的父对象是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" 为什么你有两个这样的? - Zun
我已经相应地编辑了问题,视图实际上有点更复杂,但这是问题的核心(如果我完全这样做,我会遇到同样的问题)。我使用android:visibility="gone"在启动时隐藏视图,但使用tools:visibility="visible"是因为它让我看到空视图是否在设计上OK。 - brf42
1个回答

1

1.- Put in your NestedScrollView this :

android:overScrollMode="never"

当我们使用nestedScrollView并将recyclerView放在其中时,会出现什么样的问题呢?它会根据手势以不同的速度滚动,导致滚动功能不够平滑。
2.-为解决此问题,在设置适配器后只需添加以下代码即可: ```java recyclerView.setNestedScrollingEnabled(false); ```
ViewCompat.setNestedScrollingEnabled(recyclerView, false);

现在您的RecyclerView将支持平滑滚动...

希望这对您有所帮助!


尝试使用CoordinatorLayout。 - Xavi Jimenez
是的,我已经尝试过了,我还尝试了LinearLayout,但没有成功。 - brf42
但是将您的回收器添加到嵌套中。 - Xavi Jimenez
我提供了这个答案是因为你需要它。而且我知道那个问题。这就是为什么我说了两步。你也可以在你的recycler上使用android:nestedScrollingEnabled="false"。 - Xavi Jimenez
我会将您的问题留待未来。无论如何,我会尝试找到解决方案。 - Xavi Jimenez
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接