android - 旧的回收 View 在旋转后仍保留为背景

标签 android android-recyclerview android-databinding

我正在使用带有mmvm+livedata+数据绑定(bind)的recyclerview。 每次旋转后,我都可以看到旧的元素卡在后面作为背景。

我可以看到适配器具有正确数量的项目,并且实际上适配器仅显示和滚动最新数据。但在后台您可以看到旧列表。

代码非常标准和简单 - 带有数据绑定(bind)项的简单的 adatper/recyclervyew。

最佳答案

即使您没有发布任何代码(这确实没有帮助),我仍将假设您正在 fragment 中显示您的RecyclerView,并且在您的 Activity 的 onCreate 中,您有以下行。

getSupportFragmentManager()
    .beginTransaction()
    .add(frameLayout, fragment)
    .commit();

FragmentManager 的优点之一是,即使您旋转屏幕,它也会保留 fragment ,这样内容就不会改变。只要 savedInstanceState 不为 null,它就会执行此操作。

这意味着,如果您只是通过检查保存的状态来添加 fragment ,它将被放置在管理器保存的 fragment 上,这就是您正在做的事情。然而,利用此功能来维护状态需要一些编码工作,而且我无法帮助您,因为我不知道您的代码是什么样的。因此,最简单的解决方案是将 add 更改为 replace

getSupportFragmentManager()
    .beginTransaction()
    .replace(frameLayout, fragment)
    .commit();

这告诉管理器“我不关心保存的状态。如果之前有一个 fragment ,只需在添加这个 fragment 之前将其删除即可。”

关于android - 旧的回收 View 在旋转后仍保留为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53895240/

相关文章:

android - 更改 recyclerview 适配器中的项目

android - 数据绑定(bind) BR 在我的 mvvm 项目中的 notifyPropertyChanged 方法中不起作用

android - 如何从Android数据绑定(bind)中的类导入公共(public)静态字段

Android PlaceFilter 与 Google Places API 的结合使用

android - 设置 Android 隐私政策

android - 尝试使用 keytool 为 Android Facebook 生成 key 哈希

android - 与 GoogleServices 插件并行使用 Android DataBinding 插件

android - 如何解析一个非标准的 HTTP 响应?

android - notifyDataSetChanged 不刷新 RecyclerView

java - Textview 可见性在 recyclerview 中自动创建