CardPresenter.java revision f1f489269da4b349125df56d54d3259929d48a7d
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 */
14package com.example.android.leanback;
15
16import android.content.Context;
17import android.graphics.drawable.Drawable;
18import android.support.v17.leanback.widget.ImageCardView;
19import android.support.v17.leanback.widget.Presenter;
20import android.text.TextUtils;
21import android.util.Log;
22import android.view.ViewGroup;
23import android.view.View.MeasureSpec;
24import android.view.ViewGroup.LayoutParams;
25import android.widget.TextView;
26
27import java.util.Random;
28
29public 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
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
53    public static int getRowHeight(Context context) {
54        setupRowHeights(context);
55        return sRowHeight;
56    }
57
58    public static int getExpandedRowHeight(Context context) {
59        setupRowHeights(context);
60        return sExpandedRowHeight;
61    }
62
63    @Override
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
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
97    public void onUnbindViewHolder(ViewHolder viewHolder) {
98        Log.d(TAG, "onUnbindViewHolder");
99    }
100}
101