use of com.tmall.wireless.tangram.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);
loadedMore = true;
} else {
loadSupport.doLoad(card);
}
card.loaded = true;
}
}
}
use of com.tmall.wireless.tangram.support.async.CardLoadSupport in project Tangram-Android by alibaba.
the class TangramActivity method onCreate.
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
Picasso.setSingletonInstance(new Picasso.Builder(this).loggingEnabled(true).build());
} catch (Exception e) {
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
recyclerView = (RecyclerView) findViewById(R.id.main_view);
// Step 1: init tangram
TangramBuilder.init(this, new IInnerImageSetter() {
@Override
public <IMAGE extends ImageView> void doLoadImageUrl(@NonNull IMAGE view, @Nullable String url) {
Picasso.with(TangramActivity.this).load(url).into(view);
}
}, ImageView.class);
// Tangram.switchLog(true);
mMainHandler = new Handler(getMainLooper());
// Step 2: register build=in cells and cards
builder = TangramBuilder.newInnerBuilder(this);
// Step 3: register business cells and cards
builder.registerCell(1, TestView.class);
builder.registerCell(10, SimpleImgView.class);
builder.registerCell(2, SimpleImgView.class);
builder.registerCell(4, RatioTextView.class);
builder.registerCell(110, TestViewHolderCell.class, new ViewHolderCreator<>(R.layout.item_holder, TestViewHolder.class, TextView.class));
builder.registerCell(199, SingleImageView.class);
builder.registerVirtualView("vvtest");
// Step 4: new engine
engine = builder.build();
engine.setVirtualViewTemplate(VVTEST.BIN);
engine.getService(VafContext.class).setImageLoaderAdapter(new IImageLoaderAdapter() {
private List<ImageTarget> cache = new ArrayList<ImageTarget>();
@Override
public void bindImage(String uri, final ImageBase imageBase, int reqWidth, int reqHeight) {
RequestCreator requestCreator = Picasso.with(TangramActivity.this).load(uri);
Log.d("TangramActivity", "bindImage request width height " + reqHeight + " " + reqWidth);
if (reqHeight > 0 || reqWidth > 0) {
requestCreator.resize(reqWidth, reqHeight);
}
ImageTarget imageTarget = new ImageTarget(imageBase);
cache.add(imageTarget);
requestCreator.into(imageTarget);
}
@Override
public void getBitmap(String uri, int reqWidth, int reqHeight, final Listener lis) {
RequestCreator requestCreator = Picasso.with(TangramActivity.this).load(uri);
Log.d("TangramActivity", "getBitmap request width height " + reqHeight + " " + reqWidth);
if (reqHeight > 0 || reqWidth > 0) {
requestCreator.resize(reqWidth, reqHeight);
}
ImageTarget imageTarget = new ImageTarget(lis);
cache.add(imageTarget);
requestCreator.into(imageTarget);
}
});
Utils.setUedScreenWidth(720);
// Step 5: add card load support if you have card that loading cells async
engine.addCardLoadSupport(new CardLoadSupport(new AsyncLoader() {
@Override
public void loadData(Card card, @NonNull final LoadedCallback callback) {
Log.w("Load Card", card.load);
mMainHandler.postDelayed(new Runnable() {
@Override
public void run() {
// do loading
JSONArray cells = new JSONArray();
for (int i = 0; i < 10; i++) {
try {
JSONObject obj = new JSONObject();
obj.put("type", 1);
obj.put("msg", "async loaded");
JSONObject style = new JSONObject();
style.put("bgColor", "#FF1111");
obj.put("style", style.toString());
cells.put(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
// callback.fail(false);
callback.finish(engine.parseComponent(cells));
}
}, 200);
}
}, new AsyncPageLoader() {
@Override
public void loadData(final int page, @NonNull final Card card, @NonNull final LoadedCallback callback) {
mMainHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.w("Load page", card.load + " page " + page);
JSONArray cells = new JSONArray();
for (int i = 0; i < 9; i++) {
try {
JSONObject obj = new JSONObject();
obj.put("type", 1);
obj.put("msg", "async page loaded, params: " + card.getParams().toString());
cells.put(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<BaseCell> cs = engine.parseComponent(cells);
if (card.page == 1) {
GroupBasicAdapter<Card, ?> adapter = engine.getGroupBasicAdapter();
card.setCells(cs);
adapter.refreshWithoutNotify();
Range<Integer> range = adapter.getCardRange(card);
adapter.notifyItemRemoved(range.getLower());
adapter.notifyItemRangeInserted(range.getLower(), cs.size());
} else
card.addCells(cs);
// mock load 6 pages
callback.finish(card.page != 6);
card.notifyDataChange();
}
}, 400);
}
}));
engine.addSimpleClickSupport(new SampleClickSupport());
// Step 6: enable auto load more if your page's data is lazy loaded
engine.enableAutoLoadMore(true);
// Step 7: bind recyclerView to engine
engine.bindView(recyclerView);
// Step 8: listener recyclerView onScroll event to trigger auto load more
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
engine.onScrolled();
}
});
// Step 9: set an offset to fix card
engine.getLayoutManager().setFixOffset(0, 40, 0, 0);
// Step 10: get tangram data and pass it to engine
String json = new String(getAssertsFile(this, "data.json"));
JSONArray data = null;
try {
data = new JSONArray(json);
engine.setData(data);
} catch (JSONException e) {
e.printStackTrace();
}
findViewById(R.id.first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
engine.refresh(true);
}
});
}
use of com.tmall.wireless.tangram.support.async.CardLoadSupport in project Tangram-Android by alibaba.
the class TangramDataParserActivity method onCreate.
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
Picasso.setSingletonInstance(new Picasso.Builder(this).loggingEnabled(true).build());
} catch (Exception e) {
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
recyclerView = (RecyclerView) findViewById(R.id.main_view);
// Step 1: init tangram
TangramBuilder.init(this, new IInnerImageSetter() {
@Override
public <IMAGE extends ImageView> void doLoadImageUrl(@NonNull IMAGE view, @Nullable String url) {
Picasso.with(TangramDataParserActivity.this).load(url).into(view);
}
}, ImageView.class);
// Tangram.switchLog(true);
mMainHandler = new Handler(getMainLooper());
// Step 2: register build=in cells and cards
builder = TangramBuilder.newInnerBuilder(this);
// add: custom dataParser
builder.setDataParser(new SampleDataParser());
// Step 3: register business cells and cards
builder.registerCard(1000, BannerCard.class);
builder.registerCard(2000, FourColumnCard.class);
builder.registerCell(1, TestView.class);
builder.registerCell(10, SimpleImgView.class);
builder.registerCell(2, SimpleImgView.class);
builder.registerCell(4, RatioTextView.class);
builder.registerCell(110, TestViewHolderCell.class, new ViewHolderCreator<>(R.layout.item_holder, TestViewHolder.class, TextView.class));
builder.registerCell(199, SingleImageView.class);
builder.registerVirtualView("vvtest");
// Step 4: new engine
engine = builder.build();
engine.setVirtualViewTemplate(VVTEST.BIN);
Utils.setUedScreenWidth(720);
// Step 5: add card load support if you have card that loading cells async
engine.addCardLoadSupport(new CardLoadSupport(new AsyncLoader() {
@Override
public void loadData(Card card, @NonNull final LoadedCallback callback) {
Log.w("Load Card", card.load);
mMainHandler.postDelayed(new Runnable() {
@Override
public void run() {
// do loading
JSONArray cells = new JSONArray();
for (int i = 0; i < 10; i++) {
try {
JSONObject obj = new JSONObject();
obj.put("type", 1);
obj.put("msg", "async loaded");
JSONObject style = new JSONObject();
style.put("bgColor", "#FF1111");
obj.put("style", style.toString());
cells.put(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
// callback.fail(false);
callback.finish(engine.parseComponent(cells));
}
}, 200);
}
}, new AsyncPageLoader() {
@Override
public void loadData(final int page, @NonNull final Card card, @NonNull final LoadedCallback callback) {
mMainHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.w("Load page", card.load + " page " + page);
JSONArray cells = new JSONArray();
for (int i = 0; i < 9; i++) {
try {
JSONObject obj = new JSONObject();
obj.put("type", 1);
obj.put("msg", "async page loaded, params: " + card.getParams().toString());
cells.put(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<BaseCell> cs = engine.parseComponent(cells);
if (card.page == 1) {
GroupBasicAdapter<Card, ?> adapter = engine.getGroupBasicAdapter();
card.setCells(cs);
adapter.refreshWithoutNotify();
Range<Integer> range = adapter.getCardRange(card);
adapter.notifyItemRemoved(range.getLower());
adapter.notifyItemRangeInserted(range.getLower(), cs.size());
} else
card.addCells(cs);
// mock load 6 pages
callback.finish(card.page != 6);
card.notifyDataChange();
}
}, 400);
}
}));
engine.addSimpleClickSupport(new SampleClickSupport());
// Step 6: enable auto load more if your page's data is lazy loaded
engine.enableAutoLoadMore(true);
// Step 7: bind recyclerView to engine
engine.bindView(recyclerView);
// Step 8: listener recyclerView onScroll event to trigger auto load more
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
engine.onScrolled();
}
});
// Step 9: set an offset to fix card
engine.getLayoutManager().setFixOffset(0, 40, 0, 0);
// Step 10: get tangram data and pass it to engine
String json = new String(getAssertsFile(this, "data_third.json"));
JSONArray data = null;
try {
data = new JSONArray(json);
engine.setData(data);
} catch (JSONException e) {
e.printStackTrace();
}
findViewById(R.id.first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
engine.refresh(true);
}
});
}
use of com.tmall.wireless.tangram.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);
}
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);
loadedMore = true;
} else {
loadSupport.doLoad(c);
}
c.loaded = true;
}
}
if (mEnableAutoLoadMore && mGroupBasicAdapter.getItemCount() - position < mPreLoadNumber) {
loadMoreCard();
}
}
use of com.tmall.wireless.tangram.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));
}
}
Aggregations