本文介绍了BottomSheet + ViewPager2 拖动隐藏不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got troubles and I think it is CoordinatorLayout's fault but not sure. I'm using ViewPager2 inside a ConstraintLayout and I use the CoordinatorLayout like BottomSheet. But when I drag to hide it not works good. I replaced the ViewPager2 by ViewPager and it works well. I hope you can help me.

这是我的 XML 文件:

This is my XML file:

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

推荐答案

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

This can be solved by disabling the nested scrolling of the ViewPager2 RecyclerView; but since the RecyclerView can't be directly accessed through the ViewPager2 library. Then it can't be achieved in layout.

因此,我们可以使用 Java 反射获取 RecyclerView ,如下所示:

So, instead we can get that RecyclerView using java reflection as follows:

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.

This works with me without setting the OverScrollMode, so, if it doesn't work with you can disable the OverScrollMode like above.

这篇关于BottomSheet + ViewPager2 拖动隐藏不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 02:55