• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.example.android.leanback;
15 
16 import android.content.Context;
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.text.TextUtils;
21 import android.util.Log;
22 import android.view.ViewGroup;
23 import android.view.View.MeasureSpec;
24 import android.view.ViewGroup.LayoutParams;
25 import android.widget.TextView;
26 
27 import java.util.Random;
28 
29 public class CardPresenter extends Presenter {
30     private static final String TAG = "CardPresenter";
31 
32     private static final int IMAGE_HEIGHT_DP = 120;
33 
34     private static Random sRand = new Random();
35     private static int sRowHeight = 0;
36     private static int sExpandedRowHeight = 0;
37 
setupRowHeights(Context context)38     private static void setupRowHeights(Context context) {
39         if (sRowHeight == 0) {
40             float density = context.getResources().getDisplayMetrics().density;
41             int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f);
42 
43             ImageCardView v = new ImageCardView(context);
44             v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height);
45             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
46             sRowHeight = v.getMeasuredHeight();
47             v.setActivated(true);
48             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
49             sExpandedRowHeight = v.getMeasuredHeight();
50         }
51     }
52 
getRowHeight(Context context)53     public static int getRowHeight(Context context) {
54         setupRowHeights(context);
55         return sRowHeight;
56     }
57 
getExpandedRowHeight(Context context)58     public static int getExpandedRowHeight(Context context) {
59         setupRowHeights(context);
60         return sExpandedRowHeight;
61     }
62 
63     @Override
onCreateViewHolder(ViewGroup parent)64     public ViewHolder onCreateViewHolder(ViewGroup parent) {
65         Log.d(TAG, "onCreateViewHolder");
66         ImageCardView v = new ImageCardView(parent.getContext());
67         v.setFocusable(true);
68         v.setFocusableInTouchMode(true);
69         // Randomly makes image view crop as a square or just stretch to original
70         // aspect ratio.
71         if (sRand.nextBoolean()) {
72             v.setMainImageAdjustViewBounds(false);
73             v.setMainImageDimensions(getRowHeight(parent.getContext()),
74                     getRowHeight(parent.getContext()));
75         } else {
76             v.setMainImageAdjustViewBounds(true);
77             v.setMainImageDimensions(LayoutParams.WRAP_CONTENT,
78                     getRowHeight(parent.getContext()));
79         }
80         return new ViewHolder(v);
81     }
82 
83     @Override
onBindViewHolder(ViewHolder viewHolder, Object item)84     public void onBindViewHolder(ViewHolder viewHolder, Object item) {
85         Log.d(TAG, "onBindViewHolder for " + item.toString());
86         PhotoItem photoItem = (PhotoItem) item;
87         Drawable drawable =  viewHolder.view.getContext().getResources()
88                 .getDrawable(photoItem.getImageResourceId());
89         ((ImageCardView) viewHolder.view).setMainImage(drawable);
90         ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle());
91         if (!TextUtils.isEmpty(photoItem.getContent())) {
92             ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent());
93         }
94     }
95 
96     @Override
onUnbindViewHolder(ViewHolder viewHolder)97     public void onUnbindViewHolder(ViewHolder viewHolder) {
98         Log.d(TAG, "onUnbindViewHolder");
99     }
100 }
101