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