【问题标题】:There is no way to prevent ScrollView from automatically scrolling when TextView is focused?有没有办法防止 ScrollView 在 TextView 获得焦点时自动滚动?
【发布时间】:2021-09-08 13:42:54
【问题描述】:

如果我第一次单击 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>

【问题讨论】:

    标签: android android-layout android-widget android-scrollview android-scroll


    【解决方案1】:

    您可以创建一个 ScrollView 的子类并覆盖受保护的方法:computeScrollDeltaToGetChildRectOnScreen(Rect rect) 并返回 0 以禁用自动滚动效果。

    computeScrollDeltaToGetChildRectOnScreen

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

    Kotlin 示例:

    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
        }
    }
    

    Xml 用法:

    <?xml version="1.0" encoding="utf-8"?>
    <mypackagename.com.CustomScrollView
        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>
    </mypackagename.com.CustomScrollView>
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        相关资源
        最近更新 更多