如何从嵌套的Crollview内部拖动底部表视图?

发布于 2025-02-10 23:58:32 字数 1311 浏览 2 评论 0原文

我的底部表格查看a nestedscrollview ,我想在从nestedscrollview向下滚动时拖动bottomSheet 但这是行不通的。

我的XML代码如下:

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:behavior_hideable="true"
        app:behavior_peekHeight="230dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

     <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/window_background"
            app:elevation="0dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
     
           ...

      </com.google.android.material.appbar.AppBarLayout>

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

           ...

      </androidx.core.widget.NestedScrollView>    
 
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

I have inside my BottomSheet view a NestedScrollView and I want to drag down the bottomSheet when scrolling down from the nestedScrollview but it is not working.

My XML code is like below :

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:behavior_hideable="true"
        app:behavior_peekHeight="230dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

     <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/window_background"
            app:elevation="0dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
     
           ...

      </com.google.android.material.appbar.AppBarLayout>

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

           ...

      </androidx.core.widget.NestedScrollView>    
 
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2025-02-17 23:58:32

启用了nestedscrollview的嵌套滚动,它没有拖到BottomSheet之所以拖动,该滚动已启用,这会捕获滚动事件,直到一路向上滚动为止。

禁用nestedscrollview不是一个选项,因为它在底部滚动中需要。间歇性启用/禁用可能会导致意外的滚动行为。

解决方法:解决此问题的一种选择;每当nestedScrollview检测到向上的滚动时,都会一直向上滚动,以便底部表可以折叠的机会:

// Kotlin 
nestedScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
    if (scrollY < oldScrollY) // Scroll up
        nestedScrollView.scrollTo(0, 0)
})

// Java
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView nestedScrollView, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (scrollY < oldScrollY) // Scroll up
            nestedScrollView.scrollTo(0, 0);
    }
});

用户将看到的顶部内容的下侧nestedscrollview在关闭之前;即使他们改变了不崩溃的主意。可能不是最好的选择;但这很简单并且有效。

The reason it's not dragging down the BottomSheet that the nested scrolling of the NestedScrollView is enabled which catches the scrolling event until scrolling all the way up.

Disabling the NestedScrollView is not an option because it's needed in bottom scrolling. And intermittent enabling/disabling of it can cause unexpected scrolling behavior.

Workaround: An option to solve this; is to scroll all the way up whenever the NestedScrollView detects an up scroll, so that the bottom sheet can take the chance of collapsing:

// Kotlin 
nestedScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
    if (scrollY < oldScrollY) // Scroll up
        nestedScrollView.scrollTo(0, 0)
})

// Java
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView nestedScrollView, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (scrollY < oldScrollY) // Scroll up
            nestedScrollView.scrollTo(0, 0);
    }
});

The down side that the user would see the top content of the NestedScrollView before it goes off; or even if they changed their mind of not collapsing the bottom sheet. Probably not the best option; but it is simple and works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文