Using diffutil to simplify updating the list
This commit is contained in:
parent
cd06cbf696
commit
633285a787
@ -11,6 +11,7 @@ import android.support.annotation.NonNull;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.view.ViewCompat;
|
import android.support.v4.view.ViewCompat;
|
||||||
|
import android.support.v7.util.DiffUtil;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@ -88,9 +89,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
// Preloaded images
|
// Preloaded images
|
||||||
private Bitmap mWebpageBitmap, mFolderBitmap;
|
private Bitmap mWebpageBitmap, mFolderBitmap;
|
||||||
|
|
||||||
// Bookmarks
|
|
||||||
private final List<HistoryItem> mBookmarks = new ArrayList<>();
|
|
||||||
|
|
||||||
// Views
|
// Views
|
||||||
@BindView(R.id.right_drawer_list) RecyclerView mBookmarksListView;
|
@BindView(R.id.right_drawer_list) RecyclerView mBookmarksListView;
|
||||||
@BindView(R.id.starIcon) ImageView mBookmarkTitleImage;
|
@BindView(R.id.starIcon) ImageView mBookmarkTitleImage;
|
||||||
@ -136,8 +134,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
// Handle bookmark click
|
// Handle bookmark click
|
||||||
private final OnItemClickListener mItemClickListener = new OnItemClickListener() {
|
private final OnItemClickListener mItemClickListener = new OnItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(int position) {
|
public void onItemClick(@NonNull HistoryItem item) {
|
||||||
final HistoryItem item = mBookmarks.get(position);
|
|
||||||
if (item.isFolder()) {
|
if (item.isFolder()) {
|
||||||
mScrollIndex = ((LinearLayoutManager) mBookmarksListView.getLayoutManager()).findFirstVisibleItemPosition();
|
mScrollIndex = ((LinearLayoutManager) mBookmarksListView.getLayoutManager()).findFirstVisibleItemPosition();
|
||||||
setBookmarksShown(item.getTitle(), true);
|
setBookmarksShown(item.getTitle(), true);
|
||||||
@ -149,8 +146,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
|
|
||||||
private final OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {
|
private final OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onItemLongClick(int position) {
|
public boolean onItemLongClick(@NonNull HistoryItem item) {
|
||||||
final HistoryItem item = mBookmarks.get(position);
|
|
||||||
handleLongPress(item);
|
handleLongPress(item);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -184,7 +180,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
setupNavigationButton(view, R.id.action_reading, R.id.icon_reading);
|
setupNavigationButton(view, R.id.action_reading, R.id.icon_reading);
|
||||||
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
|
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
|
||||||
|
|
||||||
mBookmarkAdapter = new BookmarkListAdapter(mBookmarks, mFaviconModel, mFolderBitmap, mWebpageBitmap);
|
mBookmarkAdapter = new BookmarkListAdapter(mFaviconModel, mFolderBitmap, mWebpageBitmap);
|
||||||
mBookmarkAdapter.setOnItemClickListener(mItemClickListener);
|
mBookmarkAdapter.setOnItemClickListener(mItemClickListener);
|
||||||
mBookmarkAdapter.setOnItemLongClickListener(mItemLongClickListener);
|
mBookmarkAdapter.setOnItemLongClickListener(mItemLongClickListener);
|
||||||
mBookmarksListView.setLayoutManager(new LinearLayoutManager(getContext()));
|
mBookmarksListView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
@ -250,11 +246,10 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleBookmarkDeleted(@NonNull HistoryItem item) {
|
public void handleBookmarkDeleted(@NonNull HistoryItem item) {
|
||||||
mBookmarks.remove(item);
|
|
||||||
if (item.isFolder()) {
|
if (item.isFolder()) {
|
||||||
setBookmarksShown(null, false);
|
setBookmarksShown(null, false);
|
||||||
} else {
|
} else {
|
||||||
mBookmarkAdapter.notifyDataSetChanged();
|
mBookmarkAdapter.deleteItem(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -289,9 +284,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setBookmarkDataSet(@NonNull List<HistoryItem> items, boolean animate) {
|
private void setBookmarkDataSet(@NonNull List<HistoryItem> items, boolean animate) {
|
||||||
mBookmarks.clear();
|
mBookmarkAdapter.updateItems(items);
|
||||||
mBookmarks.addAll(items);
|
|
||||||
mBookmarkAdapter.notifyDataSetChanged();
|
|
||||||
final int resource;
|
final int resource;
|
||||||
if (mUiModel.isRootFolder()) {
|
if (mUiModel.isRootFolder()) {
|
||||||
resource = R.drawable.ic_action_star;
|
resource = R.drawable.ic_action_star;
|
||||||
@ -376,15 +369,20 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
@BindView(R.id.textBookmark) TextView txtTitle;
|
@BindView(R.id.textBookmark) TextView txtTitle;
|
||||||
@BindView(R.id.faviconBookmark) ImageView favicon;
|
@BindView(R.id.faviconBookmark) ImageView favicon;
|
||||||
|
|
||||||
|
@NonNull private final BookmarkListAdapter adapter;
|
||||||
|
|
||||||
@Nullable private final OnItemLongClickListener onItemLongClickListener;
|
@Nullable private final OnItemLongClickListener onItemLongClickListener;
|
||||||
@Nullable private final OnItemClickListener onItemClickListener;
|
@Nullable private final OnItemClickListener onItemClickListener;
|
||||||
|
|
||||||
BookmarkViewHolder(@NonNull View itemView,
|
BookmarkViewHolder(@NonNull View itemView,
|
||||||
|
@NonNull BookmarkListAdapter adapter,
|
||||||
@Nullable OnItemLongClickListener onItemLongClickListener,
|
@Nullable OnItemLongClickListener onItemLongClickListener,
|
||||||
@Nullable OnItemClickListener onItemClickListener) {
|
@Nullable OnItemClickListener onItemClickListener) {
|
||||||
super(itemView);
|
super(itemView);
|
||||||
ButterKnife.bind(this, itemView);
|
ButterKnife.bind(this, itemView);
|
||||||
|
|
||||||
|
this.adapter = adapter;
|
||||||
|
|
||||||
this.onItemClickListener = onItemClickListener;
|
this.onItemClickListener = onItemClickListener;
|
||||||
this.onItemLongClickListener = onItemLongClickListener;
|
this.onItemLongClickListener = onItemLongClickListener;
|
||||||
|
|
||||||
@ -394,29 +392,31 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (onItemClickListener != null) {
|
int index = getAdapterPosition();
|
||||||
onItemClickListener.onItemClick(getAdapterPosition());
|
if (onItemClickListener != null && index != RecyclerView.NO_ID) {
|
||||||
|
onItemClickListener.onItemClick(adapter.itemAt(index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onLongClick(View v) {
|
public boolean onLongClick(View v) {
|
||||||
return onItemLongClickListener != null &&
|
int index = getAdapterPosition();
|
||||||
onItemLongClickListener.onItemLongClick(getAdapterPosition());
|
return index != RecyclerView.NO_POSITION && onItemLongClickListener != null &&
|
||||||
|
onItemLongClickListener.onItemLongClick(adapter.itemAt(index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OnItemLongClickListener {
|
interface OnItemLongClickListener {
|
||||||
boolean onItemLongClick(int position);
|
boolean onItemLongClick(@NonNull HistoryItem item);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OnItemClickListener {
|
interface OnItemClickListener {
|
||||||
void onItemClick(int position);
|
void onItemClick(@NonNull HistoryItem item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class BookmarkListAdapter extends RecyclerView.Adapter<BookmarkViewHolder> {
|
private static class BookmarkListAdapter extends RecyclerView.Adapter<BookmarkViewHolder> {
|
||||||
|
|
||||||
@NonNull private final List<HistoryItem> mBookmarks;
|
@NonNull private List<HistoryItem> mBookmarks = new ArrayList<>();
|
||||||
@NonNull private final FaviconModel mFaviconModel;
|
@NonNull private final FaviconModel mFaviconModel;
|
||||||
@NonNull private final Bitmap mFolderBitmap;
|
@NonNull private final Bitmap mFolderBitmap;
|
||||||
@NonNull private final Bitmap mWebpageBitmap;
|
@NonNull private final Bitmap mWebpageBitmap;
|
||||||
@ -424,11 +424,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
@Nullable private OnItemLongClickListener mOnItemLongCLickListener;
|
@Nullable private OnItemLongClickListener mOnItemLongCLickListener;
|
||||||
@Nullable private OnItemClickListener mOnItemClickListener;
|
@Nullable private OnItemClickListener mOnItemClickListener;
|
||||||
|
|
||||||
BookmarkListAdapter(@NonNull List<HistoryItem> bookmarks,
|
BookmarkListAdapter(@NonNull FaviconModel faviconModel,
|
||||||
@NonNull FaviconModel faviconModel,
|
|
||||||
@NonNull Bitmap folderBitmap,
|
@NonNull Bitmap folderBitmap,
|
||||||
@NonNull Bitmap webpageBitmap) {
|
@NonNull Bitmap webpageBitmap) {
|
||||||
mBookmarks = bookmarks;
|
|
||||||
mFaviconModel = faviconModel;
|
mFaviconModel = faviconModel;
|
||||||
mFolderBitmap = folderBitmap;
|
mFolderBitmap = folderBitmap;
|
||||||
mWebpageBitmap = webpageBitmap;
|
mWebpageBitmap = webpageBitmap;
|
||||||
@ -442,12 +440,52 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
mOnItemClickListener = listener;
|
mOnItemClickListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
HistoryItem itemAt(int position) {
|
||||||
|
return mBookmarks.get(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deleteItem(@NonNull HistoryItem item) {
|
||||||
|
List<HistoryItem> newList = new ArrayList<>(mBookmarks);
|
||||||
|
newList.remove(item);
|
||||||
|
updateItems(newList);
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateItems(@NonNull List<HistoryItem> newList) {
|
||||||
|
final List<HistoryItem> oldList = mBookmarks;
|
||||||
|
mBookmarks = newList;
|
||||||
|
|
||||||
|
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
|
||||||
|
@Override
|
||||||
|
public int getOldListSize() {
|
||||||
|
return oldList.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNewListSize() {
|
||||||
|
return mBookmarks.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||||
|
return oldList.get(oldItemPosition).equals(mBookmarks.get(newItemPosition));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||||
|
return oldList.get(oldItemPosition).equals(mBookmarks.get(newItemPosition));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
diffResult.dispatchUpdatesTo(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BookmarkViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
public BookmarkViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
||||||
View itemView = inflater.inflate(R.layout.bookmark_list_item, parent, false);
|
View itemView = inflater.inflate(R.layout.bookmark_list_item, parent, false);
|
||||||
|
|
||||||
return new BookmarkViewHolder(itemView, mOnItemLongCLickListener, mOnItemClickListener);
|
return new BookmarkViewHolder(itemView, this, mOnItemLongCLickListener, mOnItemClickListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user