SideInfoCardPresenter.java revision ad31f63f5843898de645f6ee1ac244c872ded8cc
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package android.support.v17.leanback.supportleanbackshowcase.cards.presenters;
16
17import android.content.Context;
18import android.support.v17.leanback.supportleanbackshowcase.R;
19import android.support.v17.leanback.supportleanbackshowcase.models.Card;
20import android.support.v17.leanback.widget.BaseCardView;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.widget.ImageView;
24import android.widget.TextView;
25
26import com.squareup.picasso.Picasso;
27
28/**
29 * This Presenter will display a card consisting of an image on the left side of the card followed
30 * by text on the right side. The image and text have equal width. The text will work like a info
31 * box, thus it will be hidden if the parent row is inactive. This behavior is unique to this card
32 * and requires a special focus handler.
33 */
34public class SideInfoCardPresenter extends AbstractCardPresenter<BaseCardView> {
35
36    public SideInfoCardPresenter(Context context) {
37        super(context);
38    }
39
40    @Override
41    protected BaseCardView onCreateView() {
42        final BaseCardView cardView = new BaseCardView(getContext()) {
43
44            @Override
45            public void setActivated(boolean activated) {
46                super.setActivated(activated);
47                onActivateStateChanged(this, activated);
48            }
49        };
50        cardView.setFocusable(true);
51        cardView.setCardType(BaseCardView.CARD_TYPE_MAIN_ONLY);
52        cardView.addView(LayoutInflater.from(getContext()).inflate(R.layout.side_info_card, null));
53        onActivateStateChanged(cardView, cardView.isActivated());
54        return cardView;
55    }
56
57    @Override
58    public void onBindViewHolder(Card card, BaseCardView cardView) {
59        ImageView imageView = (ImageView) cardView.findViewById(R.id.main_image);
60        if (card.getLocalImageResourceName() != null) {
61            int width = (int) getContext().getResources()
62                    .getDimension(R.dimen.sidetext_image_card_width);
63            int height = (int) getContext().getResources()
64                    .getDimension(R.dimen.sidetext_image_card_height);
65            int resourceId = getContext().getResources()
66                    .getIdentifier(card.getLocalImageResourceName(),
67                            "drawable", getContext().getPackageName());
68            Picasso.with(getContext()).load(resourceId).resize(width, height).centerCrop()
69                    .into(imageView);
70        }
71
72        TextView primaryText = (TextView) cardView.findViewById(R.id.primary_text);
73        primaryText.setText(card.getTitle());
74
75        TextView secondaryText = (TextView) cardView.findViewById(R.id.secondary_text);
76        secondaryText.setText(card.getDescription());
77
78        TextView extraText = (TextView) cardView.findViewById(R.id.extra_text);
79        extraText.setText(card.getExtraText());
80    }
81
82    public void onActivateStateChanged(final BaseCardView cardView, boolean activated) {
83        cardView.findViewById(R.id.info).setVisibility(activated ? View.VISIBLE : View.GONE);
84    }
85}
86