• 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.util.Log;
21 import android.view.ViewGroup;
22 import android.view.View.MeasureSpec;
23 import android.view.ViewGroup.LayoutParams;
24 import android.widget.TextView;
25 
26 public class CardPresenter extends Presenter {
27     private static final String TAG = "CardPresenter";
28 
29     private static final int IMAGE_HEIGHT_DP = 120;
30 
31     private static int sRowHeight = 0;
32     private static int sExpandedRowHeight = 0;
33 
setupRowHeights(Context context)34     private static void setupRowHeights(Context context) {
35         if (sRowHeight == 0) {
36             float density = context.getResources().getDisplayMetrics().density;
37             int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f);
38 
39             ImageCardView v = new ImageCardView(context);
40             v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height);
41             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
42             sRowHeight = v.getMeasuredHeight();
43             v.setActivated(true);
44             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
45             sExpandedRowHeight = v.getMeasuredHeight();
46         }
47     }
48 
getRowHeight(Context context)49     public static int getRowHeight(Context context) {
50         setupRowHeights(context);
51         return sRowHeight;
52     }
53 
getExpandedRowHeight(Context context)54     public static int getExpandedRowHeight(Context context) {
55         setupRowHeights(context);
56         return sExpandedRowHeight;
57     }
58 
59     @Override
onCreateViewHolder(ViewGroup parent)60     public ViewHolder onCreateViewHolder(ViewGroup parent) {
61         Log.d(TAG, "onCreateViewHolder");
62         ImageCardView v = new ImageCardView(parent.getContext());
63         v.setFocusable(true);
64         v.setFocusableInTouchMode(true);
65         v.setMainImageAdjustViewBounds(true);
66         v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, getRowHeight(parent.getContext()));
67         return new ViewHolder(v);
68     }
69 
70     @Override
onBindViewHolder(ViewHolder viewHolder, Object item)71     public void onBindViewHolder(ViewHolder viewHolder, Object item) {
72         Log.d(TAG, "onBindViewHolder for " + item.toString());
73         PhotoItem photoItem = (PhotoItem) item;
74         Drawable drawable =  viewHolder.view.getContext().getResources()
75                 .getDrawable(photoItem.getImageResourceId());
76         ((ImageCardView) viewHolder.view).setMainImage(drawable);
77         ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle());
78     }
79 
80     @Override
onUnbindViewHolder(ViewHolder viewHolder)81     public void onUnbindViewHolder(ViewHolder viewHolder) {
82         Log.d(TAG, "onUnbindViewHolder");
83     }
84 }
85