如何在没有数据重复的情况下使用 Intentservice 更新 recyclerview

How to update recyclerview using Intentservice without data duplicate

我有一个 recyclerview 使用 Intentservice 通过 http 请求获取数据。我有一个按钮来更新我的 recylcerview 适配器。当我按下这个按钮时,数据更新并且看起来没问题。但是当我按下这个按钮并改变屏幕方向时,recylcler 视图中的行数加倍。

我的宣言中有这个:

        <activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize">

所以看起来 android 不应该创建一个新的 activity 改变方向,当我只是改变方向而不按更新按钮时他不会。有人对此有一些想法吗?谢谢

第一次添加数据前先清空列表即可。这样它也会在您返回同一页面时为您提供帮助。

方向改变后会发生什么

方向的生命周期

onPause();
onSaveInstanceState();
onStop();
onDestroy();

onCreate();
onStart();
onResume();

使用这个

android:configChanges="keyboardHidden|orientation|screenSize"

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

http://developer.android.com/guide/topics/resources/runtime-changes.html

Post 您的 activity 代码,如果您需要更多帮助。乐于助人。

每当发现新数据时,检查旧数据。

如果列表中没有新数据,则将其插入。 如果列表中有新数据可用,则 检查数据是否相同:无事可做 如果数据不同:用新数据更新旧数据并通知 recyclerview itemupdated

您需要指定哪些数据需要更新,哪些不需要。应用您的逻辑进行比较。