gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:25:28 24 4
gpt4 key购买 nike

我有 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 - 为什么 RecyclerView 会在 View 聚焦时滚动到 View 顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45458054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com