6

I have a recycler view with horizontal scroll. Every item in recyclerview has edittext. When i click on edittext, recyclerview scroll to end. How i can forbid scrolling? Thanks!

aleksey080
  • 187
  • 4
  • 13
  • I have the same problem. I wonder why does this happen when recyclerView is horizontal, because there is no problem if it's vertical. – temirbek Aug 26 '17 at 08:59
  • i am getting the same issue, i resolved this issue by requesting focus on edittext in xml but when i scroll the recyclerview it is scrolling irrelevantly – Shashwat Gupta Aug 29 '17 at 08:44

2 Answers2

0

You could set an on focus event listener on your EditText to disable scrolling on focus and enable it again when it goes out of focus.

Something like:

edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
        recyclerview.addOnItemTouchListener(disabler);
    }else {
        recyclerview.removeOnItemTouchListener(disabler);
    }
   }
 });

Where the disabler is an instance of this class (from this answer):

public class RecyclerViewDisabler implements    RecyclerView.OnItemTouchListener {

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        return true;
    }

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

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}  
Community
  • 1
  • 1
Caleb Lewis
  • 523
  • 6
  • 20
  • This is not work for me. RecylerView scrolling to end, and focus change on first visible element(( – aleksey080 Feb 01 '16 at 13:53
  • I have tried it but nothing changes, can you go to my post to review it? [here](https://stackoverflow.com/questions/64802989/keep-state-recyclerview-when-keyboard-open) – Aldan Nov 24 '20 at 07:04
0

This can be fixed by using two listeners. The first one one disables scrolling while the user touches input field:

private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            manager.setScrollEnabled(false);
        }
    }
};

Set onFocusChangeListener to every input field of every item of your RecyclerView in onBindViewHolder() method. The second listener enables scrolling on user touch:

private RecyclerView.OnItemTouchListener onItemTouchListener = new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        RecyclerView.LayoutManager manager = rv.getLayoutManager();
        if (manager instanceof CustomLinearLayoutManager){
            ((CustomLinearLayoutManager)manager).setScrollEnabled(true);
        }
        return false;
    }

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

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
};

The code for custom LinearLayoutManager, that allows you to enable or disable scrolling is described in this answer: How to disable RecyclerView scrolling?

The trick is in overriding one method:

@Override
public boolean canScrollHorizontally() {
    return isScrollEnabled && super.canScrollHorizontally();
}

And setting the variable isScrollEnabled when you want to enable/disable scrolling behavior:

public void setScrollEnabled(boolean flag) {
    this.isScrollEnabled = flag;
}
anro
  • 1,300
  • 16
  • 30
  • I have tried it but nothing changes, can you go to my post to review it? [here](https://stackoverflow.com/questions/64802989/keep-state-recyclerview-when-keyboard-open) – Aldan Nov 24 '20 at 07:03