Add DiffUtil changePayload actions to avoid the list flickering

This commit is contained in:
Shinokuni 2019-03-06 11:58:16 +01:00
parent bc1581c4d4
commit 54d6fca734
2 changed files with 55 additions and 23 deletions

View File

@ -104,7 +104,7 @@ public class ManageFeedsActivity extends AppCompatActivity {
FeedWithFolder feedWithFolder1 = oldItem.getModel();
FeedWithFolder feedWithFolder2 = newItem.getModel();
return feedWithFolder1.getFeed().getName().equals(feedWithFolder1.getFeed().getName()) &&
return feedWithFolder1.getFeed().getName().equals(feedWithFolder2.getFeed().getName()) &&
feedWithFolder1.getFeed().getUrl().equals(feedWithFolder2.getFeed().getUrl()) &&
feedWithFolder1.getFolder().getName().equals(feedWithFolder2.getFolder().getName());
}
@ -112,6 +112,17 @@ public class ManageFeedsActivity extends AppCompatActivity {
@Nullable
@Override
public Object getChangePayload(FeedWithFolderItem oldItem, int oldItemPosition, FeedWithFolderItem newItem, int newItemPosition) {
Bundle bundle = new Bundle();
if (!oldItem.getModel().getFeed().getName().equals(newItem.getModel().getFeed().getName()))
bundle.putString(FeedWithFolderItem.FEED_NAME_KEY, newItem.getModel().getFeed().getName());
if (!oldItem.getModel().getFolder().getName().equals(newItem.getModel().getFolder().getName()))
bundle.putString(FeedWithFolderItem.FOLDER_NAME_KEY, newItem.getModel().getFolder().getName());
if (bundle.size() > 0)
return bundle;
else
return null;
}
});

View File

@ -1,5 +1,6 @@
package com.readrops.app.views;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
@ -16,6 +17,9 @@ import java.util.List;
public class FeedWithFolderItem extends ModelAbstractItem<FeedWithFolder, FeedWithFolderItem, FeedWithFolderItem.ViewHolder> {
public static final String FEED_NAME_KEY = "name";
public static final String FOLDER_NAME_KEY = "folder_name";
private ManageFeedsListener listener;
public FeedWithFolderItem(FeedWithFolder feedWithFolder) {
@ -38,9 +42,23 @@ public class FeedWithFolderItem extends ModelAbstractItem<FeedWithFolder, FeedWi
return R.layout.feed_layout;
}
@Override
public long getIdentifier() {
return getModel().getFeed().getId();
}
@Override
public void bindView(ViewHolder holder, List<Object> payloads) {
super.bindView(holder, payloads);
if (!payloads.isEmpty()) {
Bundle bundle = (Bundle)payloads.get(0);
if (bundle.getString(FEED_NAME_KEY) != null)
holder.feedName.setText(bundle.getString(FEED_NAME_KEY));
if (bundle.getString(FOLDER_NAME_KEY) != null)
holder.folderName.setText(bundle.getString(FOLDER_NAME_KEY));
} else {
FeedWithFolder feedWithFolder = getModel();
if (feedWithFolder.getFeed().getIconUrl() != null) {
@ -68,6 +86,9 @@ public class FeedWithFolderItem extends ModelAbstractItem<FeedWithFolder, FeedWi
holder.deleteFeed.setOnClickListener(v -> listener.onDelete(feedWithFolder));
}
}
public void setListener(ManageFeedsListener listener) {
this.listener = listener;
}