Search in sources :

Example 1 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method getValueBy.

@Override
public Object getValueBy(TangramExpr var) {
    if (var.hasNextFragment()) {
        String next = var.nextFragment();
        List<Card> cards = getGroupBasicAdapter().getGroups();
        for (int i = 0, size = cards.size(); i < size; i++) {
            Card card = cards.get(i);
            if (card.id.equals(next)) {
                return card.getValueBy(var);
            }
        }
    }
    return null;
}
Also used : Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Example 2 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class BannerTestActivity 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(BannerTestActivity.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(BannerTestActivity.this).load(uri);
            Log.d("BannerTestActivity", "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(BannerTestActivity.this).load(uri);
            Log.d("BannerTestActivity", "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, "testdata.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);
        }
    });
}
Also used : Listener(com.tmall.wireless.vaf.virtualview.Helper.ImageLoader.Listener) CardLoadSupport(com.tmall.wireless.tangram.support.async.CardLoadSupport) IInnerImageSetter(com.tmall.wireless.tangram.util.IInnerImageSetter) ArrayList(java.util.ArrayList) RequestCreator(com.squareup.picasso.RequestCreator) SampleClickSupport(com.tmall.wireless.tangram.example.support.SampleClickSupport) NonNull(android.support.annotation.NonNull) TextView(android.widget.TextView) RatioTextView(com.tmall.wireless.tangram.example.data.RatioTextView) List(java.util.List) ArrayList(java.util.ArrayList) GroupBasicAdapter(com.tmall.wireless.tangram.core.adapter.GroupBasicAdapter) AsyncLoader(com.tmall.wireless.tangram.support.async.AsyncLoader) TestViewHolder(com.tmall.wireless.tangram.example.data.TestViewHolder) JSONArray(org.json.JSONArray) Handler(android.os.Handler) JSONException(org.json.JSONException) AsyncPageLoader(com.tmall.wireless.tangram.support.async.AsyncPageLoader) Range(com.alibaba.android.vlayout.Range) ImageView(android.widget.ImageView) SimpleImgView(com.tmall.wireless.tangram.example.data.SimpleImgView) SingleImageView(com.tmall.wireless.tangram.example.data.SingleImageView) View(android.view.View) TestView(com.tmall.wireless.tangram.example.data.TestView) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) RatioTextView(com.tmall.wireless.tangram.example.data.RatioTextView) JSONException(org.json.JSONException) IOException(java.io.IOException) Card(com.tmall.wireless.tangram.dataparser.concrete.Card) JSONObject(org.json.JSONObject) VafContext(com.tmall.wireless.vaf.framework.VafContext) IImageLoaderAdapter(com.tmall.wireless.vaf.virtualview.Helper.ImageLoader.IImageLoaderAdapter) Picasso(com.squareup.picasso.Picasso) RecyclerView(android.support.v7.widget.RecyclerView) ImageBase(com.tmall.wireless.vaf.virtualview.view.image.ImageBase) TargetApi(android.annotation.TargetApi)

Example 3 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class RxExposureSupportTest method testDestroyExposure.

@Test
@SmallTest
@UiThreadTest
public void testDestroyExposure() {
    ExposureSupport exposureSupport = new ExposureSupport() {

        private CompositeDisposable mCompositeDisposable = new CompositeDisposable();

        Consumer<ClickExposureCellOp> consumer = new Consumer<ClickExposureCellOp>() {

            @Override
            public void accept(ClickExposureCellOp clickEvent) throws Exception {
                // should not execute here
                assertTrue(false);
                assertTrue(Looper.myLooper() == Looper.getMainLooper());
                assertEquals(clickEvent.getArg1(), mView1);
                assertEquals(clickEvent.getArg2(), mBaseCell1);
                assertEquals(clickEvent.getArg3().intValue(), 10);
                Log.d("RxExposureSupportTest", "testExposureTaskAsyncThenCancelItButResubscribe  mEventType " + clickEvent.getArg3());
                Log.d("RxExposureSupportTest", "testDestroyExposure  thread " + Thread.currentThread().getId() + ": " + Thread.currentThread().getName());
            }
        };

        @Override
        public void onExposure(@NonNull Card card, int offset, int position) {
        }
    };
    mServiceManager.register(ExposureSupport.class, exposureSupport);
    mBaseCell1.exposure(mView1);
    mBaseCell1.exposure(mView1);
    exposureSupport.destroy();
}
Also used : ExposureSupport(com.tmall.wireless.tangram.support.ExposureSupport) Consumer(io.reactivex.functions.Consumer) NonNull(android.support.annotation.NonNull) ClickExposureCellOp(com.tmall.wireless.tangram.op.ClickExposureCellOp) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Card(com.tmall.wireless.tangram.dataparser.concrete.Card) SmallTest(android.support.test.filters.SmallTest) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 4 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method insertWith.

/**
 * A high performance method to insert cells. Do not allowed to insert to an empty Tangram. TODO handle nested card
 * @param insertPosition the position to be inserted
 * @param list new cell data list
 * @since 2.1.0
 */
public void insertWith(int insertPosition, List<BaseCell> list) {
    int newItemSize = list != null ? list.size() : 0;
    if (newItemSize > 0 && mGroupBasicAdapter != null) {
        if (insertPosition >= mGroupBasicAdapter.getItemCount()) {
            insertPosition = mGroupBasicAdapter.getItemCount() - 1;
        }
        BaseCell insertCell = mGroupBasicAdapter.getItemByPosition(insertPosition);
        int cardIdx = mGroupBasicAdapter.findCardIdxFor(insertPosition);
        Card card = mGroupBasicAdapter.getCardRange(cardIdx).second;
        card.addCells(card, card.getCells().indexOf(insertCell), list);
        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, list);
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Example 5 with Card

use of com.tmall.wireless.tangram.dataparser.concrete.Card in project Tangram-Android by alibaba.

the class TangramEngine method replace.

/**
 * Replace parent card's children. Cells' size should be equal with parent's children size.
 * @param parent
 * @param cells
 * @since 2.1.0
 */
public void replace(Card parent, List<BaseCell> cells) {
    VirtualLayoutManager layoutManager = getLayoutManager();
    if (parent != null && cells != null && cells.size() > 0 && mGroupBasicAdapter != null && layoutManager != null) {
        Card card = parent;
        List<BaseCell> oldChildren = new ArrayList<>(parent.getCells());
        if (oldChildren.size() == cells.size()) {
            card.setCells(cells);
            mGroupBasicAdapter.replaceComponent(oldChildren, cells);
        } else {
            List<LayoutHelper> layoutHelpers = layoutManager.getLayoutHelpers();
            int cardIdx = mGroupBasicAdapter.findCardIdxForCard(parent);
            int diff = 0;
            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 (i < cardIdx) {
                    // do nothing
                    } else if (i == cardIdx) {
                        diff = cells.size() - layoutHelper.getItemCount();
                        layoutHelper.setItemCount(cells.size());
                        layoutHelper.setRange(start, end + diff);
                    } else {
                        layoutHelper.setRange(start + diff, end + diff);
                    }
                }
                card.setCells(cells);
                mGroupBasicAdapter.replaceComponent(oldChildren, cells);
            }
        }
    }
}
Also used : BaseCell(com.tmall.wireless.tangram.structure.BaseCell) ArrayList(java.util.ArrayList) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) Card(com.tmall.wireless.tangram.dataparser.concrete.Card)

Aggregations

Card (com.tmall.wireless.tangram.dataparser.concrete.Card)20 CardLoadSupport (com.tmall.wireless.tangram.support.async.CardLoadSupport)8 Range (com.alibaba.android.vlayout.Range)6 BaseCell (com.tmall.wireless.tangram.structure.BaseCell)6 RecyclerView (android.support.v7.widget.RecyclerView)5 View (android.view.View)5 ArrayList (java.util.ArrayList)5 JSONObject (org.json.JSONObject)5 TargetApi (android.annotation.TargetApi)4 Handler (android.os.Handler)4 NonNull (android.support.annotation.NonNull)4 TextView (android.widget.TextView)4 RatioTextView (com.tmall.wireless.tangram.example.data.RatioTextView)4 TestViewHolder (com.tmall.wireless.tangram.example.data.TestViewHolder)4 SampleClickSupport (com.tmall.wireless.tangram.example.support.SampleClickSupport)4 IInnerImageSetter (com.tmall.wireless.tangram.util.IInnerImageSetter)4 LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)3 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)3 RequestCreator (com.squareup.picasso.RequestCreator)3 GroupBasicAdapter (com.tmall.wireless.tangram.core.adapter.GroupBasicAdapter)3