RecyclerView: disable scrolling caused by a change in focus

TL DR I have a RecyclerView of EditText s. When the user focuses on EditText #1 and clicks on EditText #2 , I want EditText #2 get focus, but I don’t want to scroll through ReyclerView . How can I achieve this?


I am trying to work with a RecyclerView populated by a bunch of EditText s. When I focus on one EditText and I click on another, the RecyclerView scrolls so that the second is at the top of the screen. I want to disable this automatic scrolling with RecyclerView , but I still want the user to be able to scroll, and I still want the second EditText be focused so that the user can start typing. How can I achieve this?

I have already tried the following solutions:

  • stack overflow I called recyclerView.requestFocusFromTouch in onInterceptTouchEvent .

    • Behavior: Scrolls to the top of tapped t12 all the time.
  • Clearing the focus of any EditText when it was focused, through

     editText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, bool hasFocus) { if (hasFocus) { v.clearFocus(); } } }); 
    • Behavior: The keyboard never appeared, and the RecyclerView was still scrolling up.

Disabling scrolling in general, as in this question , is unacceptable because I still want the user to be able to scroll.

+5
source share
1 answer

I got this solution from @pskink:

 recylerView.setLayoutManager(new LinearLayoutManager(this) { @Override public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) { return false; } }); 

It seems to work fine, but @pskink mentioned that this could have problems using the arrow keys. He posted another solution here: https://pastebin.com/8JLSMkF7 . If you have problems with the above solution, you can try the alternative solution by reference. At the moment, I stick to the one I just posted here.

UPDATE

Using the v25.3.0 support library, you must also override another requestChildRectangleOnScreen method in the LayoutManager:

 @Override public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) { return false; } 
+5
source

Source: https://habr.com/ru/post/1269646/


All Articles