ViewPager2 与 SwipeRefreshLayout 冲突

Usm*_*ana 6 android android-viewpager swiperefreshlayout android-recyclerview android-viewpager2

我正在使用 Horizo​​ntal ViewPager2,里面有 4 个片段。每个片段在 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>
Run Code Online (Sandbox Code Playgroud)

Abd*_*dul 4

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

<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>
Run Code Online (Sandbox Code Playgroud)

正如我所看到的,您还有应用程序栏,因此用户约束布局并在其中包含您的应用程序栏和协调器布局,因此层次结构应该像

ConstraintLayout
  AppBar
  CoordinatorLayout
Run Code Online (Sandbox Code Playgroud)