1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.cts.jank.leanback.presenter; 18 19 import android.graphics.drawable.Drawable; 20 import android.support.v17.leanback.widget.ImageCardView; 21 import android.support.v17.leanback.widget.Presenter; 22 import android.view.ViewGroup; 23 24 import com.bumptech.glide.Glide; 25 import android.cts.jank.leanback.R; 26 import android.cts.jank.leanback.model.Movie; 27 28 /** 29 * A CardPresenter is used to generate Views and bind Objects to them on demand. 30 * It contains an Image CardView 31 */ 32 public class CardPresenter extends Presenter { 33 private static int CARD_WIDTH = 313; 34 private static int CARD_HEIGHT = 176; 35 private static int sSelectedBackgroundColor; 36 private static int sDefaultBackgroundColor; 37 private Drawable mDefaultCardImage; 38 39 @Override onCreateViewHolder(ViewGroup parent)40 public ViewHolder onCreateViewHolder(ViewGroup parent) { 41 sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background, null); 42 sSelectedBackgroundColor = 43 parent.getResources().getColor(R.color.selected_background, null); 44 mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie, null); 45 46 ImageCardView cardView = new ImageCardView(parent.getContext()) { 47 @Override 48 public void setSelected(boolean selected) { 49 updateCardBackgroundColor(this, selected); 50 super.setSelected(selected); 51 } 52 }; 53 54 cardView.setFocusable(true); 55 cardView.setFocusableInTouchMode(true); 56 updateCardBackgroundColor(cardView, false); 57 return new ViewHolder(cardView); 58 } 59 updateCardBackgroundColor(ImageCardView view, boolean selected)60 private static void updateCardBackgroundColor(ImageCardView view, boolean selected) { 61 int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor; 62 // Both background colors should be set because the view's background is temporarily visible 63 // during animations. 64 view.setBackgroundColor(color); 65 view.findViewById(R.id.info_field).setBackgroundColor(color); 66 } 67 68 @Override onBindViewHolder(Presenter.ViewHolder viewHolder, Object item)69 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 70 Movie movie = (Movie) item; 71 ImageCardView cardView = (ImageCardView) viewHolder.view; 72 73 cardView.setTitleText(movie.getTitle()); 74 cardView.setContentText(movie.getStudio()); 75 cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT); 76 Glide.with(viewHolder.view.getContext()) 77 .load(R.drawable.gradation) 78 .centerCrop() 79 .error(mDefaultCardImage) 80 .into(cardView.getMainImageView()); 81 } 82 83 @Override onUnbindViewHolder(Presenter.ViewHolder viewHolder)84 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 85 ImageCardView cardView = (ImageCardView) viewHolder.view; 86 // Remove references to images so that the garbage collector can free up memory 87 cardView.setBadgeImage(null); 88 cardView.setMainImage(null); 89 } 90 } 91