0

I want to hide keyboard if user starts scrolling in RecyclerView. dy is scrolled distance if I understood that correctly, and if it passes certain value, I can hide keyboard.

But I found out if you press and hold the finger on screen and scroll slowly, it will not change dy value (its still 1) that means my method is not working.

Anyone know other alternative how to handle this specific usecase?

Code:

list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
     override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
              if (dy.absoluteValue > KEYBOARD_HIDE_DIST) editText.clearFocus()
     }
})
martin1337
  • 2,384
  • 6
  • 38
  • 85
  • If you are using a linear layout manager you could add a listener to that and hide the keyboard on any change – cutiko Jun 22 '20 at 15:05

1 Answers1

0

Use this function computeVerticalScrollOffset() on recyclerView it will give the offset of scroll position

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
           @Override
           public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                   super.onScrolled(recyclerView, dx, dy);
                   Log.d("Scrolled","Distance Scrolled : "+ recyclerView.computeVerticalScrollOffset());
           }
       });

Not started with kotlin yet, Hence I have added java code. Hope you got the thing. Let me know if it works for you

AgentP
  • 6,261
  • 2
  • 31
  • 52