add insertComponents API at TangramEngine

This commit is contained in:
longerian
2018-01-16 20:44:32 +08:00
parent ad1794715c
commit 5e232f27c5
4 changed files with 57 additions and 19 deletions
@@ -299,7 +299,7 @@ public class BaseTangramEngine<T, C, L> implements ServiceManager {
}
/**
* Insert original data to Tangram at target position.
* Insert original data to Tangram at target position. This method will introduce page reload by notifyDataSetChanged().
* @param position Target insert position.
* @param data Original data with type {@link T}.
*/
@@ -310,7 +310,7 @@ public class BaseTangramEngine<T, C, L> implements ServiceManager {
}
/**
* Replace original data to Tangram at target position
* Replace original data to Tangram at target position. This method will introduce page reload by notifyDataSetChanged().
* @param position Target insert position.
* @param data Original data with type {@link T}.
*/
@@ -333,7 +333,7 @@ public class BaseTangramEngine<T, C, L> implements ServiceManager {
}
/**
* Append parsed data list with type {@link C} in Tangram
* Append parsed data list with type {@link C} in Tangram. This method will introduce page reload by notifyDataSetChanged().
* @param data Parsed data list.
*/
public void appendData(@Nullable List<C> data) {
@@ -342,7 +342,7 @@ public class BaseTangramEngine<T, C, L> implements ServiceManager {
}
/**
* Insert parsed data to Tangram at target position
* Insert parsed data to Tangram at target position. This method will introduce page reload by notifyDataSetChanged().
* @param position Target insert position.
* @param data Parsed data list.
*/
@@ -352,7 +352,7 @@ public class BaseTangramEngine<T, C, L> implements ServiceManager {
}
/**
* Replace original data to Tangram at target position
* Replace original data to Tangram at target position. This method will introduce page reload by notifyDataSetChanged().
* @param position Target replace position.
* @param data Parsed data list.
*/
@@ -362,7 +362,7 @@ public class BaseTangramEngine<T, C, L> implements ServiceManager {
}
/**
* Remove a card at target card position
* Remove a card at target card position. This method will introduce page reload by notifyDataSetChanged().
* @param position the position of card in group
*/
public void removeData(int position) {
@@ -371,7 +371,7 @@ public class BaseTangramEngine<T, C, L> implements ServiceManager {
}
/**
* Remove the target card from list
* Remove the target card from list. This method will introduce page reload by notifyDataSetChanged().
* @param data Target card
*/
public void removeData(C data) {
@@ -31,8 +31,8 @@ import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.Range;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.tmall.wireless.tangram.dataparser.DataParser;
@@ -497,4 +497,37 @@ public class TangramEngine extends BaseTangramEngine<JSONArray, Card, BaseCell>
}
return null;
}
/**
* A high performance method to insert cells. TODO handle nested card
* @param insertPosition the position to be inserted, note that new data will start from position + 1
* @param data new cell data list
*/
public void insertComponents(int insertPosition, List<BaseCell> data) {
int newItemSize = data != null ? data.size() : 0;
if (newItemSize > 0) {
BaseCell insertCell = mGroupBasicAdapter.getItemByPosition(insertPosition);
int cardIdx = mGroupBasicAdapter.findCardIdxFor(insertPosition);
Card card = mGroupBasicAdapter.getCardRange(cardIdx).second;
card.addCells(card, card.getCells().indexOf(insertCell) + 1, data);
List<LayoutHelper> layoutHelpers = getLayoutManager().getLayoutHelpers();
if (layoutHelpers != null && cardIdx >= 0 && cardIdx < layoutHelpers.size()) {
for (int i = 0, size = layoutHelpers.size(); i < size; i++) {
LayoutHelper layoutHelper = layoutHelpers.get(i);
int start = layoutHelper.getRange().getLower();
int end = layoutHelper.getRange().getUpper();
if (end < insertPosition) {
//do nothing
} else if (start <= insertPosition && insertPosition <= end) {
layoutHelper.setItemCount(layoutHelper.getItemCount() + newItemSize);
layoutHelper.setRange(start, end + newItemSize);
} else if (insertPosition < start) {
layoutHelper.setRange(start + newItemSize, end + newItemSize);
}
}
mGroupBasicAdapter.insertComponents(insertPosition, data);
}
}
}
}
@@ -525,17 +525,22 @@ public abstract class GroupBasicAdapter<L, C> extends VirtualLayoutAdapter<Binde
}
/**
* remove a component
* !!! Do not call this method directly. It's not designed for users.
* @param position the position to be removed
*/
abstract public void removeComponent(int position);
/**
* remove a component
* !!! Do not call this method directly. It's not designed for users.
* @param component the component to be removed
*/
abstract public void removeComponent(C component);
/**
* Do not call this method directly. It's not designed for users.
* @param pos
* @param components
*/
abstract public void insertComponents(int pos, List<C> components);
/**
@@ -425,18 +425,17 @@ public class PojoGroupBasicAdapter extends GroupBasicAdapter<Card, BaseCell> {
int newItemSize = components.size();
if (mCards != null) {
List<Pair<Range<Integer>, Card>> newCards = new ArrayList<>();
int clickPos = pos -1;
for (int i = 0, size = mCards.size(); i < size; i++) {
Pair<Range<Integer>, Card> pair = mCards.get(i);
int start = pair.first.getLower().intValue();
int end = pair.first.getUpper().intValue();
if (end < clickPos) {
int start = pair.first.getLower();
int end = pair.first.getUpper();
if (end < pos) {
//do nothing
newCards.add(pair);
} else if (start <= clickPos && clickPos <= end) {
} else if (start <= pos && pos <= end) {
Pair<Range<Integer>, Card> newPair = new Pair<>(Range.create(start, end + newItemSize), pair.second);
newCards.add(newPair);
} else if (clickPos < start) {
} else if (pos < start) {
Pair<Range<Integer>, Card> newPair = new Pair<>(Range.create(start + newItemSize, end + newItemSize), pair.second);
newCards.add(newPair);
}
@@ -444,14 +443,15 @@ public class PojoGroupBasicAdapter extends GroupBasicAdapter<Card, BaseCell> {
mCards.clear();
mCards.addAll(newCards);
}
int insertPosition = pos + 1;
for (int i = 0; i < newItemSize; i++) {
if ((pos + i) < mData.size()) {
mData.add(pos + i, components.get(i));
if ((insertPosition + i) < mData.size()) {
mData.add(insertPosition + i, components.get(i));
} else {
mData.add(components.get(i));
}
}
notifyItemRangeInserted(pos, newItemSize);
notifyItemRangeInserted(pos + 1, newItemSize);
}
}