background

According to business requirements, the current list page needs to support pull-down refresh, and each item has the requirement of sideslip. Among them, SwipeRefreshLayout is used in the drop-down refresh and SwipeLayout is used in the side swipe.

SwipeRefreshLayout and SwipeLayout will not collide when an item is in recycleView

// 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>Copy the code

SwipeRefreshLayout conflicts with SwipeLayout when an item is placed directly under SwipeRefreshLayout

<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>Copy the code


solution

  • Seeing this problem, the frames used in the two ways are the same, one will have sliding conflicts, and the other will not have sliding conflicts, so it must beRecycleViewforSwipeRefreshLayoutThere is special treatment through reading this articleBlog.csdn.net/u012250875/…It can be seen that RecycleView is added to theSwipeRefreshLayoutThe adaptation, and the solution is whenRecycleViewOnly set when you slide to the topSwipeRefreshLayoutEnable or disable at other timesSwipeRefreshLayout.
  • We can handle the overwriting of ordinary items according to the above ideaSwipeRefreshLayoutTime to cause the sliding conflict problem
  • throughSwipeLayoutProvides a listener that listens to the start and end of a slide
  • Set at the beginning of the slideSwipeRefreshLayoutDo not pull down, open after sliding
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); }});Copy the code