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.ImageCardView;
21import android.view.ContextThemeWrapper;
22
23import android.view.View;
24import android.widget.Toast;
25import com.squareup.picasso.Picasso;
26
27/**
28 * A very basic {@link ImageCardView} {@link android.support.v17.leanback.widget.Presenter}.You can
29 * pass a custom style for the ImageCardView in the constructor. Use the default constructor to
30 * create a Presenter with a default ImageCardView style.
31 */
32public class ImageCardViewPresenter extends AbstractCardPresenter<ImageCardView> {
33
34    public ImageCardViewPresenter(Context context, int cardThemeResId) {
35        super(new ContextThemeWrapper(context, cardThemeResId));
36    }
37
38    public ImageCardViewPresenter(Context context) {
39        this(context, R.style.DefaultCardTheme);
40    }
41
42    @Override
43    protected ImageCardView onCreateView() {
44        ImageCardView imageCardView = new ImageCardView(getContext());
45        imageCardView.setOnClickListener(new View.OnClickListener() {
46            @Override
47            public void onClick(View v) {
48                Toast.makeText(getContext(), "Clicked on ImageCardView", Toast.LENGTH_SHORT).show();
49            }
50        });
51        return imageCardView;
52    }
53
54    @Override
55    public void onBindViewHolder(Card card, final ImageCardView cardView) {
56        cardView.setTag(card);
57        cardView.setTitleText(card.getTitle());
58        cardView.setContentText(card.getDescription());
59        if (card.getLocalImageResourceName() != null) {
60            int resourceId = getContext().getResources()
61                    .getIdentifier(card.getLocalImageResourceName(),
62                            "drawable", getContext().getPackageName());
63            Picasso.with(getContext()).load(resourceId).into(cardView.getMainImageView());
64        }
65    }
66
67}
68