Why does RecyclerView scroll to top of view when v

2019-08-09 06:24发布

问题:

I have RecyclerView, some kind of chat. Items are TextViews The layout is reversed. Input field below RecyclerView. When I scroll to the bottom of RecyclerView and click to the bottom item it gets focused (only when it gets focused, not every click) and RecyclerView scrolls automatically to the top of that item (when the text in item is larger than screen height).

Also when the keyboard is visible and I click to RecyclerView item - keyboard hides and recycler view scrolls to the top of that item, how to disable such behavior?

I want it to stay at the same position when it was when the keyboard was opened or where it was when I clicked on some item.

Keyboard is hiding with this method

* * * * 
inputView.setOnFocusChangeListener((v, hasFocus) -> {
        if (!hasFocus){
            hideKeyboard();
        } 
    });
* * * * 

public void hideKeyboard() {
    InputMethodManager keyboard = (InputMethodManager)

    getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
}

And RecyclerView configured in such way:

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.messageList);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setReverseLayout(true);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(chatAdapter = new chatAdapter(getContext()));

If I make a button and will call hideKeyBoard() by clicking that button - its ok, it does not scroll, but when I click on RecyclerView item - it gets focused and scroll to top of that item

回答1:

I found a solution, I extended LinearLayoutManager and override some methods. Now it not scrolling to focused items. Maybe someone knows better solution?

public class NotScrollingToFocuesChildrenLinearLayoutManager extends LinearLayoutManager {
public NotScrollingToFocuesChildrenLinearLayoutManager(Context context) {
    super(context);
}

public NotScrollingToFocuesChildrenLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

public NotScrollingToFocuesChildrenLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public boolean onRequestChildFocus(RecyclerView parent, RecyclerView.State state, View child, View focused) {
    //return super.onRequestChildFocus(parent, state, child, focused);
    return true;
}

@Override
public boolean onRequestChildFocus(RecyclerView parent, View child, View focused) {
    //return super.onRequestChildFocus(parent, child, focused);
    return true;
}
}


回答2:

This answer might be your solution as well: RecyclerView scrolls to top after keyboard hides

<activity
    android:windowSoftInputMode="adjustPan"

Android Developer Site link

"adjustResize"

The activity's main window is always resized to make room for the soft keyboard on screen.

"adjustPan"

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.