gpt4 book ai didi

android - 我怎样才能将 RecyclerView 转发到它的父级 BottomSheet 以便折叠它?

转载 作者:行者123 更新时间:2023-12-03 10:12:12 25 4
gpt4 key购买 nike

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

<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>

`

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

<?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 滚动到顶部时,您的下一次滚动将由 Bottom Sheet 接管(如果您尝试再次将 recyclerview 滚动到顶部)并且触摸将移动底部的 Y 位置表。

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

在当前设置下,RecyclerView 和 ViewPager 都“吞下”了触摸,您无法从它们控制 Bottom Sheet 。如果您通过触摸 recyclerview/viewpager 上方的 View 将其下拉,您仍然可以折叠 Bottom Sheet 。

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

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

谢谢!

最佳答案

我想出了一个办法来做到这一点。我将触摸监听器附加到 RecyclerView 和 ViewPager。创建了一个连接两个 fragment (包含 Bottom Sheet 的 fragment 和 Bottom Sheet fragment )的界面。在 RecyclerView 可见的情况下,我会检查 RecyclerView 是否滚动到顶部。如果是,我会检查用户是否正在使用触摸监听器向下滑动。如果是,我将触摸转发到包含 BottomSheet 的 Fragment,并将触摸事件转发到 BottomSheet 的 Root View ,然后运行此方法:

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

在 ViewPager 的情况下,触摸监听器将无法在 ViewPager2 View 上工作(因为我猜 View 会接管触摸)所以我只是将监听器放在 ViewPager 内的 View 上并通过另一个界面运行前面的方法。

关于android - 我怎样才能将 RecyclerView 转发到它的父级 BottomSheet 以便折叠它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60111878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com