ViewPager2与SwipeRefreshLayout冲突 [英] ViewPager2 conflicting with SwipeRefreshLayout

本文介绍了ViewPager2与SwipeRefreshLayout冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用其中有4个片段的Horizo​​ntal ViewPager2.每个片段在RecyclerView上都有SwipeRefreshLayout.我面临的问题是,当RecyclerView位于最高位置时,onClick在任何项目列表上都无法正常工作.如果我稍微向下滚动RecyclerView,则onClick可以正常工作.当RecyclerView处于最高位置时,如果SwipeRefreshLayout处于刷新状态,则onClick可以正常工作.似乎SwipeRefreshLayout在触摸事件中与ViewPager2冲突.使用简单的ViewPager可以正常工作.实际的问题可能是什么,我们该如何处理?任何想法都是可取的.

I'm using Horizontal ViewPager2 which has 4 fragments inside. Each fragment has SwipeRefreshLayout on a RecyclerView. The issue that I'm facing is that when RecyclerView is at top position then onClick on any item is list is not working. If I scroll the RecyclerView a bit down then onClick works fine. And when RecyclerView is at top position then if SwipeRefreshLayout is in refreshing state then onClick works fine. It seems SwipeRefreshLayout is conflicting with ViewPager2 in touch events. It works fine with simple ViewPager. What could be the actual problem and how can we handle it? Any ideas would be appreciable.

谢谢

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>

推荐答案

我也面临相同的问题,这是由于嵌套滚动所致.经过大量搜索后,我知道应该将协调器布局用作父布局.还需要添加应用栏,然后它应该可以正常工作.它解决了这个问题.

I was also facing the same issue, it is due to nested scrolling. After a lot of searching i came to know that coordinator layout should be userd as parent layout. App bar also needs to be added and after that it should work perfectly. It resolved the issue.

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

我看到您也有应用栏,因此用户约束布局并在其中包含了应用栏和协调器布局所以层次应该像

As i see you have also app bar so user constraint layout and include your app bar and coordinator layout inside that so hierarchy should be like

ConstraintLayout
  AppBar
  CoordinatorLayout

这篇关于ViewPager2与SwipeRefreshLayout冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆