【问题标题】:ViewPager2 conflicting with SwipeRefreshLayoutViewPager2 与 SwipeRefreshLayout 冲突
【发布时间】:2020-04-09 06:43:05
【问题描述】:

我正在使用内部有 4 个片段的 Horizo​​ntal ViewPager2。每个片段在 RecyclerView 上都有 SwipeRefreshLayout。我面临的问题是,当 RecyclerView 位于顶部位置时,任何项目的 onClick 列表都不起作用。如果我将 RecyclerView 向下滚动一点,则 onClick 可以正常工作。当 RecyclerView 处于顶部位置时,如果 SwipeRefreshLayout 处于刷新状态,则 onClick 工作正常。似乎 SwipeRefreshLayout 在触摸事件中与 ViewPager2 冲突。它适用于简单的 ViewPager。什么可能是实际问题,我们该如何处理?任何想法都会很有意义。

谢谢

xml:

<androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/fragment_home"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <include
                android:id="@+id/headerPanel"
                layout="@layout/home_header"
                app:viewModel="@{viewModel}" />

            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipe_refresh_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:paddingStart="@dimen/home_post_side_spacing"
                android:paddingEnd="@dimen/home_post_side_spacing"
                app:colorScheme="@{@color/fayvo_color}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/headerPanel"
                app:onRefreshListener="@{() -> viewModel.manualRefresh()}"
                app:refreshing="@{viewModel.isRefreshing}"
                android:background="#f0f0f0">

                <RelativeLayout
                    android:id="@+id/rl_rv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/rv_homePosts"
                        adapter="@{viewModel.postObservableArrayList}"
                        addToTop="@{viewModel.addToTop}"
                        clearList="@{viewModel.clearList}"
                        showLoader="@{viewModel.isPagingEnabled &amp;&amp; viewModel.isLoading}"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

                    <FrameLayout
                        android:id="@+id/emptyFrameLayout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:visibility="@{viewModel.showEmptyLayout?View.VISIBLE:View.GONE}" />

                    <include
                        android:id="@+id/postUploadProgressLayout"
                        layout="@layout/item_post_upload_progress"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:viewModel="@{viewModel}" />

                </RelativeLayout>
            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


            <View
                android:id="@+id/tooltipView"
                android:layout_width="1dp"
                android:layout_height=".1dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                />
        </androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 你能添加你的xml吗

标签: android android-recyclerview android-viewpager swiperefreshlayout android-viewpager2


【解决方案1】:

我也遇到了同样的问题,这是由于嵌套滚动。经过大量搜索后,我知道协调器布局应该用作父布局。还需要添加应用栏,之后它应该可以完美运行。 它解决了这个问题。

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinateLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/fragmentAppBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        app:elevation="0dp" />

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvMemberList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:listitem="@layout/holder_member_item" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我看到你也有应用栏所以用户约束布局并在其中包含你的应用栏和协调器布局 所以层次结构应该像

ConstraintLayout
  AppBar
  CoordinatorLayout

【讨论】:

  • 成功了。但是 ViewPager2 应该像简单的 ViewPager 一样处理它,以保持视图层次结构简单。
  • @Abdul 我遇到了另一个问题,但这个解决方案成功了,问题是当我到达 RecyclerView 的末尾然后触摸屏幕时它会自动向上滚动,因此可以滚动再次向下(向上滚动的区域)。使用 AppBarLayout 添加 CoordinatorLayout 有助于 SwipeRefreshLayout 正常工作。
  • 非常感谢,我花了很多时间来挖掘这个问题,很好的解决方法,但仍然需要谷歌来修复这个错误。
  • 使用 ConstraintLayout -> Coordinator -> SwipeRefresh -> Recycler - 只有在 Coordinator 布局具有 height="match_parent" 时才会执行点击处理程序。如果“0dp”或“匹配约束”,则不调用点击处理程序。这对我没有好处,因为我需要 ConstraintLayout 作为父母
【解决方案2】:

一个更简单的解决方案是将 ViewPager2 的 Direct 父级从 CosntraintLayout 更改为其他一些布局,即 LinearLayout,它会正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-22
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多