【问题标题】:list starts loading from the beginning after screen rotation屏幕旋转后列表从头开始加载
【发布时间】:2021-01-14 17:26:24
【问题描述】:

在我的片段中,我有列表。这个列表是不断填充的。但是在屏幕旋转列表从头开始加载之后。垂直方向的数据丢失了,因为水平方向的列表再次开始加载。所以,我添加了所有我可以在互联网上阅读的内容: 在我的清单文件中:

android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

在 Activity 类中:

if (savedInstanceState == null) {
            firstFragment = FirstFragment()
            supportFragmentManager
                .beginTransaction()
                .add(R.id.frameLayout, firstFragment!!, "firstFragment")
                .commit()
        } else {
            firstFragment = supportFragmentManager
                .findFragmentByTag("firstFragment") as? FirstFragment
        }

但它没有帮助。

【问题讨论】:

  • 尝试使用viewModel
  • 是的,我知道这可以解决问题。但我需要在没有ViewModel 的情况下这样做

标签: android android-fragments android-recyclerview adapter onsaveinstancestate


【解决方案1】:

请在片段代码中设置setRetainInstance(true);

它将阻止重新创建片段实例。

【讨论】:

  • setRetainInstance() 现在已弃用 .. 虽然这现在可以解决问题,但将来这将不再有效 .. 您应该改用ViewModels
猜你喜欢
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多