SideInfoCardPresenter.java revision 86163d539e1aeb9ae7612def7aaf7dbfc5b88f28
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.cards.models.Card;
20import android.support.v17.leanback.supportleanbackshowcase.cards.views.BaseCardViewEx;
21import android.support.v17.leanback.supportleanbackshowcase.cards.views.OnActivateStateChangeHandler;
22import android.support.v17.leanback.widget.BaseCardView;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.widget.ImageView;
26import android.widget.TextView;
27
28import com.squareup.picasso.Picasso;
29
30/**
31 * This Presenter will display a card consisting of an image on the left side of the card followed
32 * by text on the right side. The image and text have equal width. The text will work like a info
33 * box, thus it will be hidden if the parent row is inactive. This behavior is unique to this card
34 * and requires a special focus handler.
35 */
36public class SideInfoCardPresenter extends AbstractCardPresenter<BaseCardViewEx> implements
37        OnActivateStateChangeHandler {
38
39    private static final String TAG = "SideInfoCardPresenter";
40
41    public SideInfoCardPresenter(Context context) {
42        super(context);
43    }
44
45    @Override protected BaseCardViewEx onCreateView() {
46        BaseCardViewEx cardView = new BaseCardViewEx(getContext());
47        cardView.setCardType(BaseCardView.CARD_TYPE_MAIN_ONLY);
48        cardView.addView(LayoutInflater.from(getContext()).inflate(R.layout.side_info_card, null));
49        cardView.setOnActivateStateChangeHandler(this);
50        onActivateStateChanged(cardView, cardView.isActivated());
51        return cardView;
52    }
53
54    @Override public void onBindViewHolder(Card card, BaseCardViewEx cardView) {
55        ImageView imageView = cardView.getViewById(R.id.main_image);
56        if (card.getLocalImageResourceName() != null) {
57            int width = (int) getContext().getResources()
58                                          .getDimension(R.dimen.sidetext_image_card_width);
59            int height = (int) getContext().getResources()
60                                           .getDimension(R.dimen.sidetext_image_card_height);
61            int resourceId = getContext().getResources()
62                                         .getIdentifier(card.getLocalImageResourceName(),
63                                                        "drawable", getContext().getPackageName());
64            Picasso.with(getContext()).load(resourceId).resize(width, height).centerCrop()
65                   .into(imageView);
66        }
67
68        TextView primaryText = cardView.getViewById(R.id.primary_text);
69        primaryText.setText(card.getTitle());
70
71        TextView secondaryText = cardView.getViewById(R.id.secondary_text);
72        secondaryText.setText(card.getDescription());
73
74        TextView extraText = cardView.getViewById(R.id.extra_text);
75        extraText.setText(card.getExtraText());
76    }
77
78    @Override public void onActivateStateChanged(final BaseCardViewEx cardView, boolean activated) {
79        cardView.getViewById(R.id.info).setVisibility(activated ? View.VISIBLE : View.GONE);
80    }
81}
82