我有一个嵌套的滚动视图在滚动视图内部,它可以工作,但我不希望当我在嵌套滚动视图内部滚动并到达顶部或底部时,自动继续滚动“父级”滚动视图的行为。我觉得这绝对是很烦人的。
新应用程序项目带有基本活动和内容_main.xml的内容。
新应用程序项目带有基本活动和内容_main.xml的内容。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Root"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="5dp"
android:background="@color/colorAccent"/>
<ScrollView
android:id="@+id/scrollViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark"
android:paddingRight="40dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp"
android:background="@color/colorPrimary">
<TextView
android:text="Nested1"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textViewNested1"
android:layout_margin="5dp"
android:background="@color/colorAccent"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="1000dp" android:fillViewport="true"
android:background="@android:color/black">
<LinearLayout android:layout_width="match_parent"
android:layout_height="2000dp"
android:orientation="vertical"
android:layout_margin="5dp"
android:background="@color/colorPrimaryDark">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="1800dp" android:id="@+id/textView3"
android:layout_margin="5dp"
android:background="@color/colorAccent"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<TextView
android:text="Nested2"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textViewNested2"
android:layout_margin="5dp"
android:background="@color/colorAccent"
/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="1000dp" android:fillViewport="true"
android:background="@android:color/black">
<LinearLayout android:layout_width="match_parent"
android:layout_height="2000dp"
android:orientation="vertical"
android:layout_margin="5dp"
android:background="@color/colorPrimaryDark">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="1800dp" android:id="@+id/textView4"
android:layout_margin="5dp"
android:background="@color/colorAccent"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
错误:如果顶部或底部被滚动到,内部滚动会导致父级滚动视图滚动。
正确:即使滚动到顶部或底部,内部滚动也无法滚动父级滚动视图。