[英]Nested Recyclerview scrolls by itself
I have a parent recyclerview that has 3 child view in it.我有一个父回收视图,其中包含 3 个子视图。 The last two of the child are recyclerview.
最后两个子是recyclerview。
Parent recyclerview
- child view 1
- child view 2 (horizontal rv)
- child view 3 (horizontal rv)
The issue is every time this fragment is visible, it scrolls itself to align with child view 2
's bottom.问题是每次此片段可见时,它都会自行滚动以与
child view 2
的底部对齐。
I have set the parent rv to listen for scroll.我已将父 rv 设置为侦听滚动。 This is what I end up with:
这就是我最终的结果:
dy: 108
dy: 72
dy: 75
dy: 62
dy: 48
dy: 42
dy: 34
dy: 27
dy: 22
dy: 16
dy: 12
dy: 10
dy: 7
dy: 5
dy: 3
dy: 3
dy: 1
dy: 1
dy: 1
It seems like the starting dy
of parent recyclerview is set to 0
to the child view 2
rv.似乎父 recyclerview 的起始
dy
设置为0
到child view 2
rv。 Everything above it is in -ve value.它上面的所有东西都有价值。 However, I'm not sure if this was the case as I'm still finding out what causes it.
但是,我不确定是否是这种情况,因为我仍在找出导致它的原因。
Any fix?任何修复?
We have a similar problem.我们有类似的问题。 We have a vertical
RecyclerView
.我们有一个垂直的
RecyclerView
。 Each item of this vertical RecyclerView
contains an horizontal RecyclerView
, like in the Android TV app.这个垂直
RecyclerView
每一项都包含一个水平RecyclerView
,就像在 Android TV 应用中一样。
When we upgraded the support libs from 23.4.0 to 24.0.0 the automatic scroll suddenly appeared.当我们将支持库从 23.4.0 升级到 24.0.0 时,自动滚动突然出现。 In particular, when we open an
Activity
and we then go back, the vertical RecyclerView
scrolls up so that the current horizontal RecyclerView
row does not get cut and the row is displayed completely.特别是,当我们打开一个
Activity
然后返回时,垂直RecyclerView
向上滚动,这样当前水平RecyclerView
行不会被剪切,并且该行被完整显示。
There is an easy fix.有一个简单的解决方法。 Add this to your outer/parent
RecyclerView
:将此添加到您的外部/父
RecyclerView
:
android:descendantFocusability="blocksDescendants"
I've found the solution in this questions:我在这些问题中找到了解决方案:
Additionally, I've found another solution , which also works.此外,我找到了另一个解决方案,它也有效。 In our case the vertical
RecyclerView
is contained inside a FrameLayout
.在我们的例子中,垂直
RecyclerView
包含在FrameLayout
。 If I add android:focusableInTouchMode="true"
to this FrameLayout
, the problem goes away.如果我将
android:focusableInTouchMode="true"
到这个FrameLayout
,问题就会消失。
By the way, there is also an open issue on the AOSP .顺便说一下, AOSP 上还有一个未解决的问题。
Ah, I've been struggling for a fix.啊,我一直在努力修复。 The solution is very simple actually.
解决方法其实很简单。 As a reference for me (and anyone else facing the same issue in the future), I just have to
setFocusable()
in the child view's rv to false
, and it doesn't focus to that view anymore when the fragment is visible.作为我(以及将来面临相同问题的任何其他人)的参考,我只需将子视图的 rv 中的
setFocusable()
为false
,并且当片段可见时它不再关注该视图。
In my case, I have to set it programmatically after data has been loaded from an API.就我而言,我必须在从 API 加载数据后以编程方式设置它。
Try this android:descendantFocusability="blocksDescendants"
.试试这个
android:descendantFocusability="blocksDescendants"
。 It solved the problem for me.它为我解决了这个问题。
After a long search i got my eyes on parameter reverseLayout
that was set to true.经过长时间的搜索,我看到了设置为 true 的参数
reverseLayout
。 I replaced我换了
horizontalRV.setLayoutManager(
new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true));
with与
horizontalRV.setLayoutManager(
new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
Short hint to @albertvilacalvo answer: If you use RecyclerView programmatically, you may handle setFocusableInTouchMode that way (include in your RecyclerView class)对@albertvilacalvo 的简短提示回答:如果您以编程方式使用 RecyclerView,则可以以这种方式处理 setFocusableInTouchMode(包括在您的 RecyclerView 类中)
@Override
protected void onAttachedToWindow() {
((View) getParent()).setFocusableInTouchMode(true);
super.onAttachedToWindow();
}
The descendantFocusability solution fails if you need EditText in your child views, they would not get cursor anymore.如果您的子视图中需要 EditText,则后代焦点解决方案将失败,它们将不再获得光标。 I'm using ListView, custom (multi)touch and EditText in my RecyclerView childs.
我在我的 RecyclerView 孩子中使用 ListView、自定义(多)触摸和 EditText。 No sideeffects using setFocusableInTouchMode so far.
到目前为止使用 setFocusableInTouchMode 没有副作用。
仅在 oncreate 中初始化布局管理器或仅 1 次不要重新初始化它 recyclerview.setLayoutManager(new LinearLayoutManager(context));
Try to get rid of ConstraintLayout
if you have nested RecyclerView
s.如果您嵌套了
RecyclerView
请尝试摆脱ConstraintLayout
。 I've just replaced ConstraintLayout
with good old LinearLayout
and unwanted scroll disappeared!我刚刚用旧的
LinearLayout
替换了ConstraintLayout
并且不需要的滚动消失了!
Hope this might help someone :)希望这可以帮助某人:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.