【问题标题】:How to detect which scrolling child of NestedScrollView was scrolled?如何检测 NestedScrollView 的哪个滚动子项被滚动?
【发布时间】:2019-05-29 09:57:29
【问题描述】:

我有一个包含 2 个回收站视图的父级 NestedScrollView。一个水平滚动,另一个垂直滚动。如何检测哪一个被滚动?下面是我的 xml 布局。

我无法从 NestedScrollView 中提取我的 recyclerviews,因为这个嵌套滚动视图正在协调器布局中使用,所以我必须将我的 recyclerviews 保留在 NestedScrollview 中。

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:overScrollMode="never"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollableContent"
        android:paddingTop="24dp"
        android:clipToPadding="false">

        <TextView
            style="@style/GatewayLabel"
            android:id="@+id/privateGatewaysLabel"
            android:text="@string/private_gateways_label"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/privateGatewaysRecycler"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:paddingEnd="20dp"
            android:paddingStart="20dp"
            android:clipToPadding="false"
            android:overScrollMode="never"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/privateGatewaysLabel" />

        <TextView
            android:id="@+id/sharedGatewayLabel"
            style="@style/GatewayLabel"
            android:layout_marginTop="16dp"
            android:text="@string/shared_gateways_label"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/privateGatewaysRecycler" />

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/sharedGatewaysRecycler"
            android:overScrollMode="never"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/sharedGatewayLabel" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

【问题讨论】:

    标签: android android-recyclerview android-coordinatorlayout android-nestedscrollview


    【解决方案1】:

    您可以通过将此观察者添加到您的嵌套滚动视图对象来进行检查,然后您可以采取行动

    scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                    Rect scrollBounds = new Rect();
                    scrollView.getHitRect(scrollBounds);
                    if (yourViews.getLocalVisibleRect(scrollBounds)) {  //ditect if your views visibilty is in bound or not the
                        
                    } else {
                      
                    }
    
                }
            });

    为了您的 Recyclerviews 滚动流畅度,您可以使用

     YourRecylerview.setNestedScrollingEnabled(false);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      相关资源
      最近更新 更多