gpt4 book ai didi

android - 导航回 fragment 时恢复回收器 View 状态 - 使用 jetpacks 导航架构

转载 作者:行者123 更新时间:2023-12-04 13:43:44 24 4
gpt4 key购买 nike

我正在使用最新的喷气背包导航架构。我有一个包含回收器 View 的 fragment ,这个回收器 View 显示项目列表。单击项目时,它会将您带到项目详细信息 fragment 。因此,当我从详细信息 fragment 返回时,我希望页面保留用户单击的项目的位置,而不是一直滚动到项目列表的顶部。新的喷气背包导航架构中是否有“添加” fragment 的方法?看起来 fragment 总是被“替换”?有没有办法改变这种默认行为?

最佳答案

也许为时已晚,但它可能会帮助某人

一种破解方法是将 View 存储在局部变量中,并在 onCreateView 内再次加载 fragment (返回导航后)时恢复它

假设您正在使用绑定(bind)然后

private var rootView: View? = null

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.id, container, false)

if(rootView == null) {
rootView = binding.root
return binding.root
}

return rootView


}

关于android - 导航回 fragment 时恢复回收器 View 状态 - 使用 jetpacks 导航架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52705876/

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