RecyclerView:禁用由于焦点更改而导致的滚动 [英] RecyclerView: Disable scrolling caused by change in focus
问题描述
TL; DR 我的RecyclerView
是EditText
s.当用户将焦点放在EditText #1
上并点击EditText #2
时,我希望EditText #2
获得焦点,但是我不想ReyclerView
滚动.我该如何实现?
TL;DR I have a RecyclerView
of EditText
s. When the user is focused on EditText #1
and taps on EditText #2
, I want EditText #2
to get focus but I don't want the ReyclerView
to scroll. How can I achieve this?
我正在尝试使用填充有一堆EditText
的RecyclerView
.当我专注于一个EditText
并单击另一个时,RecyclerView
滚动,以便第二个在屏幕顶部.我想从RecyclerView
禁用此自动滚动,但是我仍然希望用户能够滚动,并且我仍然希望第二个EditText
专注于此,以便用户可以开始键入.我该如何实现?
I'm trying to work with a RecyclerView
populated with a bunch of EditText
s. When I'm focused 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 auto-scrolling from the RecyclerView
, but I still want the user to be able to scroll, and I still want the second EditText
to be focused on so the user can start typing. How can I achieve this?
我已经尝试了以下解决方案:
I've already tried the following solutions:
-
https://stackoverflow.com/a/8101614/4077294 ,但带有
RecyclerView.OnItemTouchListener
.我在onInterceptTouchEvent
中叫recyclerView.requestFocusFromTouch
.
https://stackoverflow.com/a/8101614/4077294, but with a
RecyclerView.OnItemTouchListener
. I calledrecyclerView.requestFocusFromTouch
inonInterceptTouchEvent
.
- 行为:一直滚动到被点击的
EditText
顶部.
- Behavior: Scrolled to the top of the tapped
EditText
all the time.
通过
从任何EditText
集中焦点时清除焦点
Clearing the focus from any EditText
whenever it was focused on, via
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, bool hasFocus) {
if (hasFocus) {
v.clearFocus();
}
}
});
- 行为:键盘从不显示,并且
RecyclerView
仍滚动到顶部. - Behavior: The keyboard never showed up, and the
RecyclerView
still scrolled to the top.
如此问题一样,完全禁用滚动是不可接受的,因为我仍然希望用户可以滚动.
Disabling scrolling altogether as in this question is not acceptable because I still want the user to be able to scroll.
推荐答案
我最终得到了@pskink的以下解决方案:
I ended up with this solution from @pskink:
recylerView.setLayoutManager(new LinearLayoutManager(this) {
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
return false;
}
});
这似乎工作得很好,但是@pskink提到使用箭头键时可能会出现问题.他在此处发布了另一个解决方案: https://pastebin.com/8JLSMkF7 .如果您对上述解决方案有疑问,可以在链接上尝试其他解决方案.现在,我坚持只在这里发布的那篇.
It seems to work perfectly, but @pskink has mentioned that this could have problems when using arrow keys. He's posted another solution here: https://pastebin.com/8JLSMkF7. If you have problems with the above solution, you may try the alternative solution at the link. For now, I'm sticking with the one I just posted here.
更新
Since support-library v25.3.0 you should also override another requestChildRectangleOnScreen method in LayoutManager:
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
return false;
}
这篇关于RecyclerView:禁用由于焦点更改而导致的滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!