【问题标题】:How do I deactivate scrolling the parent scrollview if the nestedscrollview ends?如果nestedscrollview 结束,如何停用滚动父滚动视图?
【发布时间】:2019-10-27 01:30:59
【问题描述】:

我在滚动视图中有一个嵌套滚动视图,它正在工作,但我不希望出现这样的行为:如果我在嵌套滚动视图中滚动并且到达顶部或底部,滚动会自动继续与“父级”一起进行“滚动视图。 我觉得这非常烦人。

具有基本活动和 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>

错误:如果到达顶部或底部,则在嵌套内滚动可以滚动父滚动视图。

右:在嵌套内滚动不能滚动父滚动视图,即使达到顶部或底部

【问题讨论】:

    标签: android kotlin scrollview nestedscrollview


    【解决方案1】:

    开箱即用,我相信这是不可能的。但是,您可以创建自己的 NestedScrollView 子类并覆盖 onNestedScroll()onNestedFling() 以防止传递“未使用的”滚动值。

    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() 中,我们拦截了dxUnconsumeddyUnconsumed 并将它们重新写入0。在onNestedFling() 中,我们拦截consumed 并将其重写为true

    这使系统认为孩子一直在消耗所有的滚动,因此父级永远不会滚动响应到达边界的子滚动。

    现在我们只需要在我们的布局中使用它作为 outer 滚动视图:

    <?xml version="1.0" encoding="utf-8"?>
    <com.example.stackoverflow.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>
    

    【讨论】:

    • 请注意,这有潜在的危险:如果您的布局使得子滚动视图可能填满屏幕,则无法“退出”子滚动视图。
    • 您好,非常感谢您的回答/建议。这听起来很有道理,即使我很惊讶没有选择。当然你指出危险是正确的,这就是为什么我有一个正确的填充来确保我可以使用外部滚动。
    • 我试过你的答案,我将代码粘贴在 MainActivity.kt 的末尾并尝试替换外部滚动,然后尝试用“MyNestedScroll”替换内部嵌套滚动,但结果是同样,应用程序在启动后立即关闭。关于为什么会这样的任何想法?
    • -> 效果很好,太棒了!感谢本 P.!外部 ScrollView 已替换为我的“com.example.app05basic.MyNestedScrollView”,我第一次尝试仅使用“MyNestedScrollView”显然不起作用:)
    • 很高兴为您提供帮助:)
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多