【问题标题】:BottomSheet + ViewPager2 drag to hide not worksBottomSheet + ViewPager2 拖动隐藏不起作用
【发布时间】:2020-05-11 21:52:48
【问题描述】:

我遇到了麻烦,我认为这是CoordinatorLayout 的错,但不确定。我在ConstraintLayout 中使用ViewPager2,我使用CoordinatorLayout,如BottomSheet。但是当我拖动隐藏它时效果不佳。我用ViewPager 替换了ViewPager2,效果很好。我希望你能帮助我。

这是我的 XML 文件:

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/bottom_sheet_behavior"
        app:behavior_hideable="false"
        app:behavior_peekHeight="60dp" >

        <androidx.viewpager2.widget.ViewPager2
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="4:3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:background="#0000FF"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android android-coordinatorlayout bottom-sheet android-viewpager2


    【解决方案1】:

    这可以通过禁用ViewPager2RecyclerView的嵌套滚动来解决;但由于RecyclerView 不能通过ViewPager2 库直接访问。那么在布局上就无法实现了。

    所以,我们可以使用 java 反射得到 RecyclerView,如下所示:

    Java:

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

    科特林:

    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
    }
    

    然后禁用嵌套滚动:

    Java:

    RecyclerView recyclerView = getRecyclerView(viewPager);
    if (recyclerView != null) {
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER); // Optional
    }
    

    科特林:

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

    这适用于我,无需设置 OverScrollMode,因此,如果它不适用,您可以像上面一样禁用 OverScrollMode

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      相关资源
      最近更新 更多