Debian Codegraph @

templates/activities/AndroidTVActivity/root/src/app_package/CardPresenter.java.ftl

raw
1 /*
2 * Copyright (C) 2014 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
15 package ${packageName};
16
17 import android.graphics.drawable.Drawable;
18 import android.support.v17.leanback.widget.ImageCardView;
19 import android.support.v17.leanback.widget.Presenter;
20 import android.util.Log;
21 import android.view.ViewGroup;
22
23 import com.bumptech.glide.Glide;
24
25 /*
26 * A CardPresenter is used to generate Views and bind Objects to them on demand.
27 * It contains an Image CardView
28 */
29 public class CardPresenter extends Presenter {
30 private static final String TAG = "CardPresenter";
31
32 private static final int CARD_WIDTH = 313;
33 private static final int CARD_HEIGHT = 176;
34 private static int sSelectedBackgroundColor;
35 private static int sDefaultBackgroundColor;
36 private Drawable mDefaultCardImage;
37
38 @Override
39 public ViewHolder onCreateViewHolder(ViewGroup parent) {
40 Log.d(TAG, "onCreateViewHolder");
41
42 sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
43 sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
44 mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie);
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
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
69 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
70 Movie movie = (Movie) item;
71 ImageCardView cardView = (ImageCardView) viewHolder.view;
72
73 Log.d(TAG, "onBindViewHolder");
74 if (movie.getCardImageUrl() != null) {
75 cardView.setTitleText(movie.getTitle());
76 cardView.setContentText(movie.getStudio());
77 cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
78 Glide.with(viewHolder.view.getContext())
79 .load(movie.getCardImageUrl())
80 .centerCrop()
81 .error(mDefaultCardImage)
82 .into(cardView.getMainImageView());
83 }
84 }
85
86 @Override
87 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
88 Log.d(TAG, "onUnbindViewHolder");
89 ImageCardView cardView = (ImageCardView) viewHolder.view;
90 // Remove references to images so that the garbage collector can free up memory
91 cardView.setBadgeImage(null);
92 cardView.setMainImage(null);
93 }
94 }