【问题标题】:How can I forward scrolling the RecyclerView to its parent which is a BottomSheet in order to collapse it?如何将 RecyclerView 向前滚动到其作为 BottomSheet 的父级以折叠它?
【发布时间】:2020-02-07 10:46:13
【问题描述】:

基本上我有一个具有特定布局的片段,它有 CoordinatorLayout 作为它的父级和一个带有 BottomSheetBehaviour 的静态片段(还有一些与问题无关的其他视图) `

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/main_activity_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       Some other views....

        <fragment
            android:id="@+id/home_apps_widget"
            android:name="apps.calo.justLauncher.fragments.HomeAppsWidget"
            android:layout_width="0dp"
            android:layout_height="57dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <fragment
        android:id="@+id/bottom_sheet_container"
        android:name="apps.calo.justLauncher.fragments.AppDrawerFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/app_drawer_fragament"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        tools:layout="@layout/app_drawer_fragament" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

`

在静态片段中,我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/app_drawer_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/app_drawer_background">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/app_drawer_indicator"
            android:visibility="invisible"
            android:layout_below="@id/app_search_widget"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="5dp" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/app_list_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:itemCount="4"
            android:clipToPadding="false"
            app:spanCount="4"
            tools:listitem="@layout/app_item"
            android:layout_above="@id/app_drawer_indicator"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            android:orientation="vertical"
            android:layout_below="@id/app_search_widget"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="5dp" />

        <apps.calo.justLauncher.view.Pager2Indicator
            android:id="@+id/app_drawer_indicator"
            android:layout_width="wrap_content"
            android:layout_height="11dp"
            android:layout_alignParentBottom="true"
            app:piv_radius="3dp"
            app:selectedColorCustom="page_indicator_selected"
            app:tabIndicatorHeight="3dp"
            app:unselectedColorCustom="page_indicator_unselected" />

    </RelativeLayout>
</layout>

使用以下设置,您只能有一个活动(recyclerview 或 viewpager2),可以使用 SharedPreferences 进行更改。

我的目标是,当您将 RecyclerView 滚动到顶部时,您的下一个滚动将被底部工作表接管(如果您尝试再次将 recyclerview 滚动到顶部)并且触摸会移动底部的 Y 位置表。

我想要与 viewpager 相同的东西,但在这里你不必滚动到顶部,因为它不可滚动,所以基本上如果你向下滚动,底部的工作表会接管触摸。

在当前设置下,RecyclerView 和 ViewPager 都会“吞下”触摸,您无法控制它们的底部工作表。如果您通过触摸 recyclerview/viewpager 上方的视图将其拉下,您仍然可以折叠底部工作表。

我的问题是:如何将 recyclerview/viewpager 的触摸委托给 BottomSheet 以控制 BottomSheet 的 Y 位置。

有趣的是,当我将 ViewPager2 更改为 ViewPager 时,我使用 RecyclerView 获得了想要的效果,但使用 ViewPager 却没有。

谢谢!

【问题讨论】:

    标签: android android-fragments android-recyclerview android-viewpager bottom-sheet


    【解决方案1】:

    我想出了一个办法。我在 RecyclerView 和 ViewPager 上附加了一个触摸监听器。创建了一个连接两个片段(包含底部工作表和底部工作表片段的片段)的接口。在 RecyclerView 可见的情况下,我会检查 RecyclerView 是否滚动到顶部。如果是,我会检查用户是否正在使用触摸侦听器向下滑动。如果是,我将触摸转发到包含 BottomSheet 的 Fragment,并使用 BottomSheet 的根视图转发触摸事件,然后运行此方法:

    fun forwardTouchToBottomSheet(event: MotionEvent, view: View) {
            bottomSheetBehavior.onTouchEvent(cor, view, event)
        }
    

    在 ViewPager 的情况下,触摸监听器在 ViewPager2 视图上不起作用(因为我猜是视图会接管触摸)所以我只是将监听器放在 ViewPager 内的视图上,并通过另一个接口运行以前的方法。

    【讨论】:

    • 这个解决方案有效吗?
    猜你喜欢
    • 2016-04-01
    • 2020-09-05
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多