ViewPager2 如何与 SwipeRefreshLayout 进行交互

9

我有一个包含4个片段的viewpager2。其中3个片段具有SwipeRefreshLayout,以刷新特定片段的异步任务数据。

使用SwipeRefreshLayout和viewpager2时手势会出现冲突。例如,向下滑动以刷新使屏幕变得非常敏感,稍微向左或右移动也会更改页面屏幕并导致刷新图标冻结或进程未完成。

我的目标是使手势独立,例如,当我开始向下滑动SwipeRefreshLayout时,vp2被禁用,以避免它与SRL干扰。

在使用标准viewpager和SwipeRefreshLayout时,这种情况不会发生,手势不会发生冲突,但我需要在VP2中使用"setUserInputEnabled"。有没有什么方法可以减轻这种行为,并且我应该在SwipeRefreshLayout级别还是在viewpager2代码内部解决它?

3个回答

7

当我将以下内容添加到我的滚动视图中时,问题似乎已经解决:

android:nestedScrollingEnabled="true"

最终的片段布局代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/some_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/activity_background"
tools:context="some_fragment_name">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:nestedScrollingEnabled="true" <<<<------ HERE the change
    android:id="@+id/some_id">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sensors_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="some_package_name">

    <TextView
        android:id="@+id/some_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:layout_marginTop="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:textSize="20sp" />

...


1
你是否已经通过将该属性添加到“ScrollView”来解决了它?如果我的布局中没有父级“ScrollView”,但是遇到完全相同的问题,那该怎么办? - romandrahan
更新。我也能够通过以下方式解决它:args.object.android.setNestedScrollingEnabled(true)对于SwipeRefreshLayout中的ListViewViewPager - romandrahan
是的,你可以将其添加到ScrollView属性中,并且它可以工作。自从我使用过它以来,我的滚动行为非常棒。 - B.Ondrej
如果您只有一个RecyclerViewSwipeRefreshLayout作为子项,那该怎么办?设置nestedScrollingEnabled="true"是不起作用的。即使我开始了刷新(垂直滑动)或垂直滚动,我仍然可以水平地滑动页面... - l33t
@l33t 你试过使用最新版本的SwipeRefreshLayout吗?看看我刚写的答案。 - FlorianT

6

编辑: 2020年7月22日更新为1.1.0版本。

问题是由SwipeRefreshLayout中的一个错误引起的,这个问题已经在1.1.0版本中得到解决。只需将以下行添加到Gradle文件的依赖项中,即可升级到该版本:

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

请查看 issueTracker 以获取错误历史记录:[ViewPager2] SwipeRefreshLayout 不应忽略 requestDisallowInterceptTouchEvent。请注意,那里也有一个解决方法(扩展 SwipeRefreshLayout 并覆盖 "requestDisallowInterceptTouchEvent")。

3

如果您在SwipeRefreshLayout中使用了RecyclerView,则需要将RecyclerView包装在FrameLayout或其他布局中,并在布局上设置nestedScrollingEnabled="true",而不是在RecyclerView上设置。

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/media_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </FrameLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

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