How to remove horizontal recyclerview(child) inside the recyclerview(parent) scroll to first position when notifyitemchange?

huangapple go评论53阅读模式
英文:

How to remove horizontal recyclerview(child) inside the recyclerview(parent) scroll to first position when notifyitemchange?

问题

我在RecyclerView中使用多种视图类型。其中一种视图类型是具有水平方向的线性LayoutManagerRecyclerView。我在父RecyclerView中加载了所有视图类型。当我更改位置为10的水平RecyclerView项中的某个项目并使用adapter?.notifyItemChanged(10)更新时,水平RecyclerView会跳到第一个位置0。这会导致糟糕的用户体验。

此外,我在父和子RecyclerView中使用了android:descendantFocusability="blocksDescendants"setHasFixedSize(true),但并没有起作用。有没有解决这个问题的方法?我还附上了参考图像 How to remove horizontal recyclerview(child) inside the recyclerview(parent) scroll to first position when notifyitemchange?

英文:

I am using multiple view type inside the recyclerview. In which one of the view type is recyclerview of linear layoutmanager with horizontal orientation. I loaded all view type inside the parent recyclerview. When I change the some item in horizonatal recyclerview item of position 10 and update by adapter?.notifyItemChanged(10), the horizonatal recyclerview jump to first position 0. And it creates a bad user experience.

Also I used android:descendantFocusability="blocksDescendants" and setHasFixedSize(true) in parent and child Recyclerview not does not works. Is there is any way to solve my problem. And also attached image for reference How to remove horizontal recyclerview(child) inside the recyclerview(parent) scroll to first position when notifyitemchange?

Here position 1 is horizonal recyclerview

答案1

得分: 1

你可以保存RecyclerView的布局实例,并在通知适配器后进行恢复,这将恢复布局管理器的属性,如滚动位置。

val savedInstance = recyclerView.layoutManager?.onSaveInstanceState()
adapter?.notifyItemChanged(10)
recyclerView.layoutManager?.onRestoreInstanceState(savedInstance)
英文:

You could save the RecyclerView's layout instance and restore post notifying adapter, this will restore layout manager properties like scroll position

val savedInstance = recyclerView.layoutManager?.onSaveInstanceState()
adapter?.notifyItemChanged(10)
recyclerView.layoutManager?.onRestoreInstanceState(savedInstance)

huangapple
  • 本文由 发表于 2023年5月22日 18:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定