Android - RecyclerView - How do I temporarily prevent scrolling?

958 views Asked by At

I have a RecyclerView that I wish to freeze (ie, not allow the user to scroll, but still be able to tap any current visible views) until a certain condition is met. How do I achieve this?

I've tried to set an OnScrollListener as following:

@Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {

     super.onScrolled(recyclerView, dx, dy);

     if(myCondition){
         recyclerView.stopScroll();
     }
});

without success.

1

There are 1 answers

0
Bitcoin Cash - ADA enthusiast On BEST ANSWER

I've figured it out. OnItemTouchListener should be used instead:

onItemTouchListener = new OnItemTouchListener() {

      @Override
      public boolean onInterceptTouchEvent(final RecyclerView recyclerView, final MotionEvent e) {

          if(myCondition){

              switch(e.getAction()){

                  case MotionEvent.ACTION_MOVE: return true;
              }
          }

          return false;
      }

      @Override
      public void onTouchEvent(final RecyclerView recyclerView, final MotionEvent e) {
      }
};

Now you add it to your RecyclerView:

yourRecyclerView.addOnItemTouchListener(onItemTouchListener);

If anyone finds an easier way, please, let me know!