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