有没有办法阻止 ScrollView 在 TextView 聚焦时自动滚动?

问题描述

如果我第一次单击 text2 或在单击 text1 后单击 text2,ScrollView 似乎正在将 text2 滚动到顶部。如果我将 android:descendantFocusability="blocksDescendants" 放在 LinearLayout 中,则不会发生这种滚动,但我无法选择文本。

我想要的很简单:我不想要这种自动滚动。禁用 ScrollView 的这种行为是不可能的,我必须从头开始创建自己的滚动视图类吗?如果不可能,请将其写为答案,这样我就可以放弃了。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <TextView
                    android:text="I am text 1. Click me."
                    android:textColor="@color/black"
                    android:textIsSelectable="true"
                    android:background="#FF0000"
                    android:layout_width="match_parent"
                    android:layout_height="400dp"/>
            <TextView
                    android:text="I am text 2. Click me."
                    android:textIsSelectable="true"
                    android:background="#00FF00"
                    android:textColor="@color/black"
                    android:layout_width="match_parent"
                    android:layout_height="1000dp"/>
        </LinearLayout>
</ScrollView>

enter image description here

解决方法

android:descendantFocusability="blocksDescendants" 将阻止其后代接收焦点。

尝试在 2 个文本视图之间添加一个虚拟视图并查看。

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:focusable="true"
    android:focusableInTouchMode="true" />
,

您可以创建 ScrollView 的子类并覆盖受保护的方法:tag_name 并返回 0 以禁用自动滚动效果。

computeScrollDeltaToGetChildRectOnScreen

计算在 Y 方向滚动的量以获得一个 完全在屏幕上的矩形(或者,如果比屏幕高,在 至少是它的第一个屏幕大小块)。

Kotlin 示例:

computeScrollDeltaToGetChildRectOnScreen(Rect rect)

XML 用法:

class CustomScrollView : ScrollView {
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?,attrs: AttributeSet?) : super(context,attrs) {}
    constructor(context: Context?,attrs: AttributeSet?,defStyleAttr: Int) : super(context,attrs,defStyleAttr) {}

    override fun computeScrollDeltaToGetChildRectOnScreen(rect: Rect): Int {
        return 0
    }
}