3

I'm using a recyclerview inside a SwipeRefreshLayout and when I start to swipe to delete my recycler view item it will active pull to refresh. I want to disable pull refresh when I start swipe and it enable completed swipe. And I used ItemTouchHelper class to detect swipe action

This is my .xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 
   android:id="@+id/swipeRefreshLayout"       
   android:layout_width="match_parent"
   android:layout_height="match_parent">

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

      <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

    </RelativeLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

And, This Java file,

 new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

        }
    }).attachToRecyclerView(recyclerView);
Zoe
  • 27,060
  • 21
  • 118
  • 148
Nimantha
  • 165
  • 3
  • 11

3 Answers3

6
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            swiperefreshLayout.setEnabled(false);
        } else {
            swiperefreshLayout.setEnabled(true);
        }
    }
}
DuoBlood
  • 71
  • 1
  • 5
1

You can disable refresh layout in onMove method

swiperefreshLayout.setEnabled(false);

And then enable it in onSwiped method

swiperefreshLayout.setEnabled(true);
Hamid Reza
  • 624
  • 7
  • 23
1

Personally I'm doing this:

protected void initRecyclerViewOnItemTouchListener(){
        mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                if (e.getAction() == MotionEvent.ACTION_UP || (((LinearLayoutManager) mRecyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0x0 &&
                        e.getY() - mRecyclerView.getY() <= mRecyclerView.getHeight() / 0x4)) {
                    msrlReload.setEnabled(true);
                } else {
                    msrlReload.setEnabled(false);
                }
                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
    }
         

Using this code the "swipe to refresh" is enabled only if the recycler view is on his start position, so it can't be scrolled up more. Then you have a "slice" of the recycler view on which the refresh layout is enabled, in this case this slice is recyclerview.height / 0x4.

So only if you touch the top of the recycler view and you scroll down the refresh will start, if you are already scrolling the recycler view and you reach the top and you still scroll up the swipe won't start (this was the problem I had).

I tryed the solutions above, but still have the same problem: if I start to scroll down the list and then I scroll up to the top and beyond the swipe to refresh still starting.

I hope this is helpful for you, have a nice coding and day :D

Bye!

Z3R0
  • 1,011
  • 10
  • 19