Tangram-Android
Tangram-Android copied to clipboard
PojoGroupBasicAdapter replaceComponent bug
replace操作里面没有处理end == index 的情况,造成replace后,条目丢失
@Override
public void replaceComponent(Card oldGroup, Card newGroup) {
if (mData != null && mCards != null && oldGroup != null && newGroup != null) {
List<BaseCell> oldComponent = oldGroup.getCells();
List<BaseCell> newComponent = newGroup.getCells();
int index = mData.indexOf(oldComponent.get(0));
if (index >= 0) {
if (mCards != null) {
List<Pair<Range<Integer>, Card>> newCards = new ArrayList<>();
int diff = 0;
for (int i = 0, size = mCards.size(); i < size; i++) {
Pair<Range<Integer>, Card> pair = mCards.get(i);
int start = pair.first.getLower();
int end = pair.first.getUpper();
if (end < index) {
//do nothing
newCards.add(pair);
} else if (start <= index && index < end) {
diff = newComponent.size() - oldComponent.size();
Pair<Range<Integer>, Card> newPair = new Pair<>(Range.create(start, end + diff), newGroup);
newCards.add(newPair);
} else if (index <= start) {
Pair<Range<Integer>, Card> newPair = new Pair<>(Range.create(start + diff, end + diff), pair.second);
newCards.add(newPair);
}
}
mCards.clear();
mCards.addAll(newCards);
}
oldGroup.removed();
newGroup.added();
mData.removeAll(oldComponent);
mData.addAll(index, newComponent);
int oldSize = oldComponent.size();
int newSize = newComponent.size();
notifyItemRangeChanged(index, Math.max(oldSize, newSize));
}
}
}
遇到同样的问题,你是怎么处理?
自己改了一份内部用
自己改了一份内部用
能不能代码贡献下,参考下