如何在BottomSheetDialogFragment内的ViewPager2上启用拖动? [英] How to enable dragging on ViewPager2 inside BottomSheetDialogFragment?

查看:70
本文介绍了如何在BottomSheetDialogFragment内的ViewPager2上启用拖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There's a BottomSheetDialogFragment and working good vertically dragging states on fragment layout and opening STATE_EXPANDED mode. There's a recyclerview inside it and dragging vertically works on the bottom sheet but it doesn't work on recyclerview because of scrolling event. How the bottom sheet dragging event to work instead of scroll event on recyclerview when reached top of list and still scrolling up for collapse the bottom sheet?

BottomSheetDialogFragment 层次结构:

BottomSheetDialogFragment hierarchy:

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

BottomSheetDialogFragment xml:

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 拖动以隐藏无效

Edit: The issue is on ViewPager2, when I change it to ViewPager dragging is working good on it. Same issue: BottomSheet + ViewPager2 drag to hide not works

推荐答案

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

The issue is that we need to disable the nested scrolling on the ViewPager2, but the android:nestedScrollingEnabled="false" doesn't work because ViewPager2 is functioning internally using RecyclerView which has the effect of the nested scrolling.

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

The main issue is that the ViewPager2 RecyclerView is not accessible by default. The good news is that you can access it using java reflection.

这需要知道 RecyclerView 的名称字段,可以在 ViewPager2 定义类是 mRecyclerView

And this requires to know the name field of the RecyclerView which can be found in the ViewPager2 definition class which is mRecyclerView

将它们组合在一个辅助函数中:

Combining that together in a helper 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;
}

然后您可以按如下方式禁用嵌套滚动:

Then you can disable the nested scrolling as follows:

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

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

That worked for me, but if still there are issues, then try to disable the over scroll mode:

recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);

对于 Kotlin 用户:

扩展功能:

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

预览:

这篇关于如何在BottomSheetDialogFragment内的ViewPager2上启用拖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆