android - 为什么当 View 聚焦时 RecyclerView 会滚动到 View 顶部

标签 android scroll android-recyclerview keyboard

我有RecyclerView,某种聊天。项目与 TextViews 的布局相反。 RecyclerView 下方的输入字段。当我滚动到 RecyclerView 的底部并单击底部项目时,它会获得焦点(仅当它获得焦点时,而不是每次单击时),并且 RecyclerView 会自动滚动到顶部该项目(当项目中的文本大于屏幕高度时)。

此外,当键盘可见并且我单击RecyclerView项目时 - 键盘隐藏并且回收器 View 滚动到该项目的顶部,如何禁用这种行为?

我希望它在打开键盘时或单击某些项目时保持在同一位置。

使用此方法隐藏键盘

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

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

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

并且RecyclerView以这种方式配置:

    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()));

如果我制作一个按钮并通过单击该按钮来调用 hideKeyBoard() - 没关系,它不会滚动,但是当我单击 RecyclerView 项目时 - 它会获得焦点并滚动到顶部该项目

最佳答案

我找到了一个解决方案,我扩展了 LinearLayoutManager 并重写了一些方法。现在它不会滚动到焦点项目。也许有人知道更好的解决方案?

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;
}
}

关于android - 为什么当 View 聚焦时 RecyclerView 会滚动到 View 顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45458054/

相关文章:

android - 如何使用 material.BottomNavigationView 设置 Jetpack 导航

java - 从 onLocationMethod 获取 ConcurrentModificationException

通过代码控制鼠标滚轮

java - RecyclerView不可见

android - RecyclerView 仅显示一项

android - 如何使用回收站 View 的支持版本而不是Androidx

java - Android Studio - keystore 被篡改,或密码不正确

android - 如何为应用程序的 TalkBack 设置不同的标签?

extjs - 面板内的面板 : autoScroll not working

jquery - 防止绝对位置。页脚 div 滚动时抖动