繁体   English   中英

如何在 BottomSheetDialogFragment 内的 ViewPager2 上启用拖动?

[英]How to enable dragging on ViewPager2 inside BottomSheetDialogFragment?

有一个BottomSheetDialogFragment并且在片段布局和打开STATE_EXPANDED模式时工作良好的垂直拖动状态。 它里面有一个recyclerview ,垂直拖动可以在底部工作表上工作,但由于滚动事件,它不能在recyclerview上工作。 当到达列表顶部并且仍然向上滚动以折叠底部工作表时,底部工作表拖动事件如何工作而不是recyclerview上的滚动事件?

BottomSheetDialogFragment 层次结构:

FragmentRootLinearLayout -> ...BottomLinearLayout... -> ViewPager2 -> RecyclerView

BottomSheetDialogFragment xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/BookInfoFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/tool_sheet_bg"
    android:orientation="vertical"
    app:layout_behavior="@string/bottom_sheet_behavior"
    app:behavior_hideable="true"
    android:clickable="true"
    android:focusable="true">

    <LinearLayout
        android:id="@+id/tabs_linear_layout"
        style="@style/ThemeSettingsRowContainer"
        android:layout_width="match_parent"
        android:layout_height="550dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/xml_rounded_corner_bg2"
        android:clickable="true"
        android:focusable="true"
        android:paddingTop="0dp"
        android:paddingBottom="0dp">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/book_loading_tablayout"
            android:layout_width="match_parent"
            android:layout_height="50dp" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/book_loading_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            android:focusable="true" />

    </LinearLayout>

</LinearLayout>

编辑:问题出在 ViewPager2 上,当我将其更改为 ViewPager 时,拖动效果很好。 同样的问题: BottomSheet + ViewPager2 拖动隐藏不起作用

问题是我们需要禁用ViewPager2上的嵌套滚动,但是android:nestedScrollingEnabled="false"不起作用,因为ViewPager2在内部使用具有嵌套滚动效果的RecyclerView运行。

主要问题是ViewPager2 RecyclerView默认情况下不可访问。 好消息是您可以使用java 反射访问它。

这需要知道可以在ViewPager2定义 class 中找到的RecyclerView的名称字段,即mRecyclerView

将它们组合在一个助手 function 中:

public static RecyclerView getRecyclerView(ViewPager2 viewPager) {
    try {
        Field field = ViewPager2.class.getDeclaredField("mRecyclerView");
        field.setAccessible(true);
        return (RecyclerView) field.get(viewPager);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

然后您可以禁用嵌套滚动,如下所示:

RecyclerView recyclerView = getRecyclerView(viewPager);
if (recyclerView != null)
    recyclerView.setNestedScrollingEnabled(false);

这对我有用,但如果仍然存在问题,请尝试禁用过度滚动模式:

recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);

对于 Kotlin 用户:

扩展 function:

fun ViewPager2.getRecyclerView(): RecyclerView? {
    try {
        val field = ViewPager2::class.java.getDeclaredField("mRecyclerView")
        field.isAccessible = true
        return field.get(this) as RecyclerView
    } catch (e: NoSuchFieldException) {
        e.printStackTrace()
    } catch (e: IllegalAccessException) {
        e.printStackTrace()
    }
    return null
}

和用法:

val recyclerView = viewPager.getRecyclerView()
recyclerView?.isNestedScrollingEnabled = false
recyclerView?.overScrollMode = View.OVER_SCROLL_NEVER // Optional

预习:

如果您使用 STATE_EXPANDED 模式是默认模式并且可隐藏属性为 true,则不能将其向下拖动。

因此将 BottomSheetBehavior.STATE_COLLAPSED 设置为默认模式并设置 hideable,PeakHight 属性。

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/BookInfoFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bottom_sheet_background"
        android:orientation="vertical"
        app:layout_behavior="@string/bottom_sheet_behavior"
        
        app:behavior_peekHeight="500dp"
         app:behavior_hideable="false"

        android:clickable="true"
    android:focusable="true">

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM