CardPresenter.java revision 5e6f438acadce3d0b3fb64d695e6b0860f0a7082
181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/*
281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * Copyright (C) 2017 The Android Open Source Project
381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *
481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * you may not use this file except in compliance with the License.
681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * You may obtain a copy of the License at
781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *
881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *
1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * Unless required by applicable law or agreed to in writing, software
1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * See the License for the specific language governing permissions and
1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent * limitations under the License.
1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent */
1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpackage com.android.test.uibench.leanback;
1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.content.Context;
1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.graphics.drawable.Drawable;
2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.support.v17.leanback.widget.ImageCardView;
2181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.support.v17.leanback.widget.Presenter;
2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.support.v4.content.res.ResourcesCompat;
2381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.view.ContextThemeWrapper;
2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.view.ViewGroup;
2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentimport android.view.ViewGroup.LayoutParams;
2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic class CardPresenter extends Presenter {
2881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private int mImageWidth = 0;
3081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private int mImageHeight = 0;
31bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
326acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    public CardPresenter(int width, int height) {
336acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        mImageWidth = width;
341bfe09a0b1755a79abd32b41c0dd433b88fc260cGlenn Kasten        mImageHeight = height;
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    @Override
3897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    public ViewHolder onCreateViewHolder(ViewGroup parent) {
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Context context = parent.getContext();
4072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ImageCardView v = new ImageCardView(context);
4172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        v.setFocusable(true);
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        v.setFocusableInTouchMode(true);
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        v.setMainImageAdjustViewBounds(true);
44cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten        v.setMainImageDimensions(mImageWidth, mImageHeight);
45cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten        return new ViewHolder(v);
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    @Override
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PhotoItem photoItem = (PhotoItem) item;
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ImageCardView cardView = (ImageCardView) viewHolder.view;
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        cardView.setTitleText(photoItem.getTitle());
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        BitmapLoader.loadBitmap(cardView.getMainImageView(), photoItem.getId(),
541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                mImageWidth, mImageHeight);
551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
561c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
571c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    @Override
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public void onUnbindViewHolder(ViewHolder viewHolder) {
5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ImageCardView cardView = (ImageCardView) viewHolder.view;
601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        BitmapLoader.cancel(cardView.getMainImageView());
611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent}
631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent