use of com.tmall.wireless.tangram3.support.async.CardLoadSupport in project Tangram-Android by alibaba.
the class TangramEngine method loadFirstPageCard.
/**
* Loading data for card whose's position in within 0 to {{@link #mPreLoadNumber}}.
*/
public void loadFirstPageCard() {
if (!mEnableLoadFirstPageCard) {
return;
}
final CardLoadSupport loadSupport = getService(CardLoadSupport.class);
if (loadSupport == null) {
return;
}
boolean loadedMore = false;
List<Card> cards = mGroupBasicAdapter.getGroups();
for (int i = 0; i < Math.min(mPreLoadNumber, cards.size()); i++) {
Card card = cards.get(i);
if (!TextUtils.isEmpty(card.load) && !card.loaded) {
// page load
if (card.loadMore && !loadedMore) {
// only load one load more card
loadSupport.loadMore(card);
loadSupport.reactiveDoLoadMore(card);
loadedMore = true;
} else {
loadSupport.doLoad(card);
loadSupport.reactiveDoLoad(card);
}
card.loaded = true;
}
}
}
use of com.tmall.wireless.tangram3.support.async.CardLoadSupport in project Tangram-Android by alibaba.
the class TangramEngine method loadMoreCard.
public void loadMoreCard() {
CardLoadSupport loadSupport = getService(CardLoadSupport.class);
if (loadSupport == null) {
return;
}
List<Card> groups = findGroups(new Predicate<Card>() {
@Override
public boolean isMatch(Card data) {
return data.loadMore && data.hasMore && !data.loading && !TextUtils.isEmpty(data.load);
}
});
if (groups.size() != 0) {
loadSupport.loadMore(groups.get(groups.size() - 1));
loadSupport.reactiveDoLoadMore(groups.get(groups.size() - 1));
}
}
use of com.tmall.wireless.tangram3.support.async.CardLoadSupport in project Tangram-Android by alibaba.
the class TangramEngine method onScrolled.
/**
* Call this method in RecyclerView's scroll listener. Would trigger the preload of card's data.
*/
public void onScrolled() {
// due to a bug in 21: https://code.google.com/p/android/issues/detail?id=162753, which cause getDecoratedStart() throws NullPointException
// officially reported it has been fixed in v22
final int lastPosition = getLayoutManager().findLastVisibleItemPosition();
final int firstPosition = getLayoutManager().findFirstVisibleItemPosition();
int lastCardIndex = -1;
int firstCardIndex = -1;
int position = lastPosition;
// find the last visible item in card
for (int i = lastPosition; i >= firstPosition; i--) {
lastCardIndex = mGroupBasicAdapter.findCardIdxFor(i);
if (lastCardIndex >= 0) {
position = i;
break;
}
}
for (int i = firstCardIndex; i <= lastPosition; i++) {
firstCardIndex = mGroupBasicAdapter.findCardIdxFor(i);
if (firstCardIndex >= 0) {
break;
}
}
if (lastCardIndex < 0 || firstCardIndex < 0)
return;
final CardLoadSupport loadSupport = getService(CardLoadSupport.class);
if (loadSupport == null)
return;
List<Card> cards = mGroupBasicAdapter.getGroups();
// check the loadmore state of current card first range is inclusive-exclusive
Card current = cards.get(lastCardIndex);
Pair<Range<Integer>, Card> pair = mGroupBasicAdapter.getCardRange(lastCardIndex);
if (pair != null && position >= pair.first.getUpper() - mPreLoadNumber) {
// async load
if (!TextUtils.isEmpty(current.load) && current.loaded) {
// page load
if (current.loadMore) {
loadSupport.loadMore(current);
loadSupport.reactiveDoLoadMore(current);
}
return;
}
}
boolean loadedMore = false;
for (int i = firstCardIndex; i < Math.min(lastCardIndex + mPreLoadNumber, cards.size()); i++) {
Card c = cards.get(i);
// async load
if (!TextUtils.isEmpty(c.load) && !c.loaded) {
// page load
if (c.loadMore && !loadedMore) {
// only load one load more card
loadSupport.loadMore(c);
loadSupport.reactiveDoLoadMore(c);
loadedMore = true;
} else {
loadSupport.doLoad(c);
loadSupport.reactiveDoLoad(c);
}
c.loaded = true;
}
}
if (mEnableAutoLoadMore && mGroupBasicAdapter.getItemCount() - position < mPreLoadNumber) {
loadMoreCard();
}
}
Aggregations