如果 nestedscrollview 结束,如何停用滚动父滚动视图?
How do I deactivate scrolling the parent scrollview if the nestedscrollview ends?
我在滚动视图中有一个 nestedscrollview,它正在工作,但我不希望这样的行为,即如果我在 nestedscrollview 中滚动并到达顶部或底部,滚动会自动继续"parent" 滚动视图。
我觉得这很烦人。
具有基本 activity 和 content_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>
错误:如果到达顶部或底部,在嵌套内滚动可以滚动父滚动视图。
正确:即使到达顶部或底部,在嵌套内滚动也无法滚动父滚动视图
开箱即用,我相信这是不可能的。但是,您可以创建自己的 NestedScrollView
子类并覆盖 onNestedScroll()
和 onNestedFling()
以防止传递 "unconsumed" 滚动值。
class MyNestedScrollView(context: Context, attrs: AttributeSet?) : NestedScrollView(context, attrs) {
override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int) {
super.onNestedScroll(target, dxConsumed, dyConsumed, 0, 0, type)
}
override fun onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean): Boolean {
return super.onNestedFling(target, velocityX, velocityY, true)
}
}
在 onNestedScroll()
中,我们将 dxUnconsumed
和 dyUnconsumed
以及 re-writing 拦截到 0
。在 onNestedFling()
中,我们将 consumed
和 re-writing 拦截到 true
。
这使系统认为子级始终消耗了所有滚动,因此父级永远不会滚动以响应触及边界的子级滚动。
现在我们只需将其用作我们布局中的 outer 滚动视图:
<?xml version="1.0" encoding="utf-8"?>
<com.example.Whosebug.MyNestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- ... -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="your height here">
<!-- ... -->
</android.support.v4.widget.NestedScrollView>
<!-- ... -->
</LinearLayout>
</com.example.playground.MyNestedScrollView>
我在滚动视图中有一个 nestedscrollview,它正在工作,但我不希望这样的行为,即如果我在 nestedscrollview 中滚动并到达顶部或底部,滚动会自动继续"parent" 滚动视图。 我觉得这很烦人。
具有基本 activity 和 content_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>
错误:如果到达顶部或底部,在嵌套内滚动可以滚动父滚动视图。
正确:即使到达顶部或底部,在嵌套内滚动也无法滚动父滚动视图
开箱即用,我相信这是不可能的。但是,您可以创建自己的 NestedScrollView
子类并覆盖 onNestedScroll()
和 onNestedFling()
以防止传递 "unconsumed" 滚动值。
class MyNestedScrollView(context: Context, attrs: AttributeSet?) : NestedScrollView(context, attrs) {
override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int) {
super.onNestedScroll(target, dxConsumed, dyConsumed, 0, 0, type)
}
override fun onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean): Boolean {
return super.onNestedFling(target, velocityX, velocityY, true)
}
}
在 onNestedScroll()
中,我们将 dxUnconsumed
和 dyUnconsumed
以及 re-writing 拦截到 0
。在 onNestedFling()
中,我们将 consumed
和 re-writing 拦截到 true
。
这使系统认为子级始终消耗了所有滚动,因此父级永远不会滚动以响应触及边界的子级滚动。
现在我们只需将其用作我们布局中的 outer 滚动视图:
<?xml version="1.0" encoding="utf-8"?>
<com.example.Whosebug.MyNestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- ... -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="your height here">
<!-- ... -->
</android.support.v4.widget.NestedScrollView>
<!-- ... -->
</LinearLayout>
</com.example.playground.MyNestedScrollView>