Permalink
Browse files

Fucking cool item add/delete/move animation

  • Loading branch information...
xingrz committed Nov 1, 2014
1 parent 922af05 commit 915f6b2b177700c73b8fad91cc2f461f26b9f909
@@ -135,8 +135,7 @@ protected void deliverResponse(TopicList response) {
if (topicsAdapter.equals(response.data)) {
Toast.makeText(MainActivity.this, R.string.no_update, Toast.LENGTH_SHORT).show();
} else {
topicsAdapter.clear();
topicsAdapter.addAll(response.data);
topicsAdapter.replaceWith(response.data);
}
}
@@ -2,9 +2,11 @@
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
@@ -147,26 +149,36 @@ public boolean remove(Object object) {
@Override
public boolean removeAll(@NonNull Collection<?> collection) {
boolean modified = false;
for (int i = 0; i < list.size(); i++) {
if (collection.contains(list.get(i))) {
list.remove(i);
notifyItemRemoved(i);
Iterator<E> iterator = list.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
if (collection.contains(object)) {
int index = list.indexOf(object);
iterator.remove();
notifyItemRemoved(index);
modified = true;
}
}
return modified;
}
@Override
public boolean retainAll(@NonNull Collection<?> collection) {
boolean modified = false;
for (int i = 0; i < list.size(); i++) {
if (!collection.contains(list.get(i))) {
list.remove(i);
notifyItemRemoved(i);
Iterator<E> iterator = list.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
if (!collection.contains(object)) {
int index = list.indexOf(object);
iterator.remove();
notifyItemRemoved(index);
modified = true;
}
}
return modified;
}
@@ -202,7 +214,49 @@ public int size() {
@Override
public boolean equals(Object o) {
return list.equals(o);
return o instanceof List && list.equals(o);
}
public void replaceWith(List<E> data) {
if (list.isEmpty() && data.isEmpty()) {
return;
}
if (list.isEmpty()) {
addAll(data);
return;
}
if (data.isEmpty()) {
clear();
return;
}
// 首先将旧列表有、新列表没有的从旧列表去除
retainAll(data);
// 如果列表被完全清空了,那就直接全部插入好了
if (list.isEmpty()) {
addAll(data);
return;
}
// 然后遍历新列表,对旧列表的数据更新、移动、增加
for (int indexNew = 0; indexNew < data.size(); indexNew++) {
E item = data.get(indexNew);
int indexOld = indexOf(item);
if (indexOld == -1) {
add(indexNew, item);
} else if (indexOld == indexNew) {
set(indexNew, item);
} else {
list.remove(indexOld);
list.add(indexNew, item);
notifyItemMoved(indexOld, indexNew);
}
}
}
}

0 comments on commit 915f6b2

Please sign in to comment.