禁用 ViewPager 2 中的垂直滚动

3
我正在使用带有选项卡布局的viewPager2,只有2个片段。
在我的第二个片段中有一个RecyclerView和一个下拉刷新布局。
我的问题是当我尝试向下滑动以进行刷新时,它会与Viewpager的垂直滑动发生冲突,导致无法正确地向下滑动。
因此,是否有一种方法可以仅禁用视图Pager的垂直滑动? 我仍然希望能够水平滑动。

你找到解决方案了吗? - Radwa
不,我回到了ViewPager1。但是有一个答案。 - Tanzim Chowdhury
谢谢,我会尝试的。 - Radwa
Tanzim,@Radwa请看一下我的回答,希望有所帮助。 - Khaled Qasem
2个回答

1
您可以通过将isUserInputEnabled设置为false来禁用viewpager2的滚动。 viewPager2.isUserInputEnabled = false 这将禁用所有事件,但在您的情况下,我认为您只需要禁用向下移动操作,您需要拦截触摸侦听器并在视图页面上调用requestDisallowInterceptTouchEvent(true)
您可以使用google样本中的包装器。

/**
 * Layout to wrap a scrollable component inside a ViewPager2. Provided as a solution to the problem
 * where pages of ViewPager2 have nested scrollable elements that scroll in the same direction as
 * ViewPager2. The scrollable element needs to be the immediate and only child of this host layout.
 *
 * This solution has limitations when using multiple levels of nested scrollable elements
 * (e.g. a horizontal RecyclerView in a vertical RecyclerView in a horizontal ViewPager2).
 */
class NestedScrollableHost : FrameLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    private var touchSlop = 0
    private var initialX = 0f
    private var initialY = 0f
    private val parentViewPager: ViewPager2?
        get() {
            var v: View? = parent as? View
            while (v != null && v !is ViewPager2) {
                v = v.parent as? View
            }
            return v as? ViewPager2
        }

    private val child: View? get() = if (childCount > 0) getChildAt(0) else null

    init {
        touchSlop = ViewConfiguration.get(context).scaledTouchSlop
    }

    private fun canChildScroll(orientation: Int, delta: Float): Boolean {
        val direction = -delta.sign.toInt()
        return when (orientation) {
            0 -> child?.canScrollHorizontally(direction) ?: false
            1 -> child?.canScrollVertically(direction) ?: false
            else -> throw IllegalArgumentException()
        }
    }

    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
        handleInterceptTouchEvent(e)
        return super.onInterceptTouchEvent(e)
    }

    private fun handleInterceptTouchEvent(e: MotionEvent) {
        val orientation = parentViewPager?.orientation ?: return

        // Early return if child can't scroll in same direction as parent
        if (!canChildScroll(orientation, -1f) && !canChildScroll(orientation, 1f)) {
            return
        }

        if (e.action == MotionEvent.ACTION_DOWN) {
            initialX = e.x
            initialY = e.y
            parent.requestDisallowInterceptTouchEvent(true)
        } else if (e.action == MotionEvent.ACTION_MOVE) {
            val dx = e.x - initialX
            val dy = e.y - initialY
            val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL

            // assuming ViewPager2 touch-slop is 2x touch-slop of child
            val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f else 1f
            val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f

            if (scaledDx > touchSlop || scaledDy > touchSlop) {
                if (isVpHorizontal == (scaledDy > scaledDx)) {
                    // Gesture is perpendicular, allow all parents to intercept
                    parent.requestDisallowInterceptTouchEvent(false)
                } else {
                    // Gesture is parallel, query child if movement in that direction is possible
                    if (canChildScroll(orientation, if (isVpHorizontal) dx else dy)) {
                        // Child can scroll, disallow all parents to intercept
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        // Child cannot scroll, allow all parents to intercept
                        parent.requestDisallowInterceptTouchEvent(false)
                    }
                }
            }
        }
    }
}

然后将您的视图页面内容 -recyclerView- 包装起来,


    <androidx.viewpager2.integration.testapp.NestedScrollableHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/first_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF" />
    </androidx.viewpager2.integration.testapp.NestedScrollableHost>

0

禁用ViewPager2滚动 禁用ViewPager2用户输入滚动。

import androidx.viewpager2.widget.ViewPager2;
    private ViewPager2 viewPager;
    viewPager = findViewById(R.id.view_pager);
    viewPager.setUserInputEnabled(false);

2
这会禁用垂直和水平滚动条,而不仅仅是垂直的。 - Yago Rey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接