【问题标题】:viewpager2 interaction with SwipeRefreshLayoutviewpager2 与 SwipeRefreshLayout 的交互
【发布时间】:2019-08-03 07:19:38
【问题描述】:

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

当使用 SwipeRefreshLayout 和 viewpager2 时,手势会发生冲突。 IE。向下滑动刷新会使屏幕如此敏感,向左或向右稍微移动也会使页面屏幕发生变化,刷新图标冻结或进程未完成。

我的目标是让手势独立,例如,当我开始向下滑动 SwipeRefreshLayout 时,vp2 被禁用,因此它不会干扰 SRL。

使用带有 SwipeRefreshLayout 的标准 viewpager 时没有发生这种情况,手势不冲突,但我需要在 VP2 中使用“setUserInputEnabled”。知道如何缓解这种行为,我应该在 SwipeRefreshLayout 级别还是在 vipager2 代码中缓解它?

【问题讨论】:

    标签: android swiperefreshlayout android-viewpager2


    【解决方案1】:

    编辑:更新为 1.1.0 已于 2020 年 7 月 22 日发布

    问题是由于 SwipeRefreshLayout 中的一个错误造成的,该错误已在 1.1.0 版中得到解决。要使用它,只需通过在 Gradle 文件的依赖项中添加以下行来升级到该版本:

    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
    

    请参阅 issueTracker 了解错误历史记录:[ViewPager2] SwipeRefreshLayout should not ignore requestDisallowInterceptTouchEvent。请注意,此处还描述了一种解决方法(扩展 SwipeRefreshLayout 并覆盖“requestDisallowInterceptTouchEvent”)。

    【讨论】:

      【解决方案2】:

      当我添加到我的滚动视图时,看起来问题已经解决了:

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

      ...

      【讨论】:

      • 您是否可以通过将属性添加到ScrollView 来解决它?如果我的布局中没有父 ScrollView,但遇到完全相同的问题怎么办。
      • UPD。也可以使用 ``` args.object.android.setNestedScrollingEnabled(true) ``` 来解决它,对于 ListViewSwipeRefreshLayoutViewPager
      • 是的,您可以将其添加到 ScrollView 属性中,并且可以正常工作。自从我使用它后,我的滚动行为就非常棒了
      • 如果您的孩子只有RecyclerViewSwipeRefreshLayout 怎么办?设置nestedScrollingEnabled="true" 不起作用。即使我开始刷新(垂直滑动)或垂直滚动​​,我仍然可以(水平)滑动页面...
      • @l33t 您尝试过使用最新版本的 SwipeRefreshLayout 吗?看看我刚刚写的答案
      【解决方案3】:

      如果您在SwipeRefreshLayout 中有RecyclerView,则需要将RecyclerView 包装在FrameLayout aur 任何其他布局中,并将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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-20
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        • 1970-01-01
        • 2014-06-19
        相关资源
        最近更新 更多