SwipeRefreshLayout和SwipeLayout的滑动冲突处理

998 阅读1分钟

背景

根据业务的需求,目前列表页面需要支持下拉刷新,并且每个item有侧滑的需求。其中下拉刷新采用的是谷歌的SwipeRefreshLayout,侧滑采用的是SwipeLayout

当item放在recycleView的时候,SwipeRefreshLayoutSwipeLayout不会出现滑动冲突

// main.xml
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycleView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
 
// item.xml                
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/id_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="test" />

    <TextView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:text="test" />
</com.daimajia.swipe.SwipeLayout>

当item直接放在SwipeRefreshLayout下的时候,SwipeRefreshLayoutSwipeLayout会出现滑动冲突

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/item_home" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


解法

  • 看到这个问题,两种方式使用的框架是相同的,一个会有滑动冲突,一个没有滑动冲突,那说明肯定是RecycleView对于SwipeRefreshLayout有特殊的处理,通过这篇文章的阅读 blog.csdn.net/u012250875/…,可以看出来RecycleView是加入了对SwipeRefreshLayout的适配的,而且解决思路就是当RecycleView滑动到最顶端的时候才设置SwipeRefreshLayout enable,其他时候disable SwipeRefreshLayout
  • 根据上面的思路我们就可以处理普通item外层覆盖SwipeRefreshLayout时候引起的滑动冲突问题了
  • 通过SwipeLayout提供的监听,监听滑动的开始和结束
  • 在滑动开始的时候设置SwipeRefreshLayout禁止下拉,滑动结束后开启
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
                Log.d(TAG, "onStartOpen: ");
                refreshLayout.setEnabled(false);
            }

            @Override
            public void onOpen(SwipeLayout layout) {
                Log.d(TAG, "onOpen: ");
            }

            @Override
            public void onStartClose(SwipeLayout layout) {
                Log.d(TAG, "onStartClose: ");
                refreshLayout.setEnabled(false);
            }

            @Override
            public void onClose(SwipeLayout layout) {
                Log.d(TAG, "onClose: ");
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
                Log.d(TAG, "onUpdate: ");
            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
                Log.d(TAG, "onHandRelease: ");
                refreshLayout.setEnabled(true);
            }
        });