如果我第一次单击 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>
您可以创建 ScrollView 的子类并覆盖受保护的方法:computeScrollDeltaToGetChildRectOnScreen(Rect rect)
并返回 0 以禁用自动滚动效果。
computeScrollDeltaToGetChildRectOnScreen
计算在 Y 方向滚动的量,以便在屏幕上完全显示一个矩形(或者,如果比屏幕高,至少是它的第一个屏幕大小块)。
科特林示例:
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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句