Android prevent nested recyclerview from automatically repositioning [duplicate]

北城余情 提交于 2020-01-10 03:48:05

问题


I have a ViewPager as an item in a Linear Layout, with each item in the view pager containing a RecyclerView. The problem is, whenever the page initially loads, or when I change pages on the ViewPager, the view automatically scrolls so the beginning of the recycler view is the top of the screen, rather than staying how it is.

A .gif of the issue is here. Note I am not scrolling down, that is the automatic repositioning.

How can I prevent the view from automatically repositioning to the start of the recycler view?


回答1:


you can add this attribute to your linear layout. The Linear Layout direct child of your scrollView.

    android:descendantFocusability="blocksDescendants"



回答2:


We have a similar problem. We have a vertical RecyclerView. Each item of this vertical RecyclerView contains an horizontal RecyclerView, like in the Android TV app.

When we upgraded the support libs from 23.4.0 to 24.0.0 the automatic scroll suddenly appeared. 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.

Adding android:descendantFocusability="blocksDescendants" fixes the issue.

However, I've found another solution, which also works. In our case the vertical RecyclerView is contained inside a FrameLayout. If I add android:focusableInTouchMode="true" to this FrameLayout, the problem goes away.

There's even a third solution mentioned here, which basically consists on calling setFocusable(false) on the child/inner RecyclerViews. I haven't tryied this.

By the way, there is an open issue on the AOSP.




回答3:


I guess that the problem is not on your recyclerview but on your viewpager. ViewPager has setOffscreenPageLimit method that used to define offscreen page of your fragment attached to viewpager. if you have 3 fragment attached on viewpager, you can set its parameter like this

viePager.setOffscreenPageLimit(3); 

Whenever you change the pager page, its fragment would still on last state.



来源:https://stackoverflow.com/questions/37782485/android-prevent-nested-recyclerview-from-automatically-repositioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!