【问题标题】:Android - Screen scrolls automatically to RecyclerView when loadedAndroid - 加载时屏幕自动滚动到 RecyclerView
【发布时间】:2021-10-28 16:37:35
【问题描述】:

我正在开发一个 Android (Java) 应用程序。我有一个片段,上面有一个段落,下面是一个RecyclerView。当我启动应用程序并加载屏幕时,内容会滚动,以便顶部段落完全超出屏幕,并且RecyclerView 是唯一可见的内容。

按照这个解决方案:Recycler View automatically scrolls to inner recycler view on first load 我尝试添加

android:descendantFocusability="blocksDescendants"

focusable="false"

到回收站视图,但它们都不起作用。

这是片段的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:id="@+id/intro"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is a welcome text"
                android:textSize="15sp"
                android:lineSpacingExtra="5dp"
                android:paddingTop="10dp"
                android:paddingStart="8dp"
                android:paddingEnd="8dp"
                android:paddingBottom="16dp"
                android:textColor="#000000"
                />

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

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/pathsRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:nestedScrollingEnabled="false"
                    android:descendantFocusability="blocksDescendants"
                    />

            </RelativeLayout>



        </LinearLayout>
    </ScrollView>


</LinearLayout>

如何解决页面不自动滚动的问题?

【问题讨论】:

  • 这可能是因为滚动视图环绕着回收器视图。当一个可滚动视图包装另一个可滚动视图时,我看到了与 android 类似的问题。您是否尝试过删除滚动视图?
  • 你想要这样的东西:youtu.be/nUnPoxo_ZG4?

标签: android android-recyclerview scroll scrollview android-scrollview


【解决方案1】:

您将android:descendantFocusability 添加到RecyclerView,尽管您需要禁用ScrollView 子级的焦点功能,因为这种自动滚动是由ScrollView 引起的,而不是RecyclerView .

因此,要解决此问题,您需要将 android:descendantFocusability="blocksDescendants"RecyclerView 转移到 ScrollView 的直接子代,即 LinearLayout

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        ....
        android:descendantFocusability="blocksDescendants">

        <TextView
           ...

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 2013-01-23
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多