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.app.Activity;
17 import android.content.Intent;
18 import android.os.Bundle;
19 import android.support.v17.leanback.widget.TitleHelper;
20 import android.support.v17.leanback.widget.TitleView;
21 import android.support.v4.app.ActivityOptionsCompat;
22 import android.support.v17.leanback.R;
23 import android.support.v17.leanback.widget.ArrayObjectAdapter;
24 import android.support.v17.leanback.widget.HeaderItem;
25 import android.support.v17.leanback.widget.ImageCardView;
26 import android.support.v17.leanback.widget.ListRow;
27 import android.support.v17.leanback.widget.ListRowPresenter;
28 import android.support.v17.leanback.widget.OnItemViewClickedListener;
29 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
30 import android.support.v17.leanback.widget.Presenter;
31 import android.support.v17.leanback.widget.Row;
32 import android.support.v17.leanback.widget.RowPresenter;
33 import android.util.Log;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 
38 public class RowsFragment extends android.support.v17.leanback.app.RowsFragment {
39 
40     private static final String TAG = "leanback.RowsFragment";
41 
42     private static final int NUM_ROWS = 10;
43     // Row heights default to wrap content
44     private static final boolean USE_FIXED_ROW_HEIGHT = false;
45 
46     private ArrayObjectAdapter mRowsAdapter;
47     private TitleHelper mTitleHelper;
48 
setTitleHelper(TitleHelper titleHelper)49     public void setTitleHelper(TitleHelper titleHelper) {
50         mTitleHelper = titleHelper;
51     }
52 
53     @Override
onCreate(Bundle savedInstanceState)54     public void onCreate(Bundle savedInstanceState) {
55         Log.i(TAG, "onCreate");
56         super.onCreate(savedInstanceState);
57 
58         setupRows();
59         setOnItemViewClickedListener(new ItemViewClickedListener());
60         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
61             @Override
62             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
63                     RowPresenter.ViewHolder rowViewHolder, Row row) {
64                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
65                 if (mTitleHelper != null) {
66                     mTitleHelper.showTitle(getAdapter() == null || getAdapter().size() == 0 ||
67                             getAdapter().get(0) == row);
68                 }
69             }
70         });
71     }
72 
setupRows()73     private void setupRows() {
74         ListRowPresenter lrp = new ListRowPresenter();
75 
76         // For good performance, it's important to use a single instance of
77         // a card presenter for all rows using that presenter.
78         final CardPresenter cardPresenter = new CardPresenter();
79 
80         if (USE_FIXED_ROW_HEIGHT) {
81             lrp.setRowHeight(cardPresenter.getRowHeight(getActivity()));
82             lrp.setExpandedRowHeight(cardPresenter.getExpandedRowHeight(getActivity()));
83         }
84 
85         mRowsAdapter = new ArrayObjectAdapter(lrp);
86 
87         for (int i = 0; i < NUM_ROWS; ++i) {
88             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
89             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
90             listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
91             listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3));
92             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
93             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5));
94             listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_6));
95             listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_7));
96             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_8));
97             HeaderItem header = new HeaderItem(i, "Row " + i);
98             mRowsAdapter.add(new ListRow(header, listRowAdapter));
99         }
100 
101         setAdapter(mRowsAdapter);
102     }
103 
104     private final class ItemViewClickedListener implements OnItemViewClickedListener {
105         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)106         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
107                 RowPresenter.ViewHolder rowViewHolder, Row row) {
108             Intent intent = new Intent(getActivity(), DetailsActivity.class);
109             intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);
110 
111             Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
112                     getActivity(),
113                     ((ImageCardView)itemViewHolder.view).getMainImageView(),
114                     DetailsActivity.SHARED_ELEMENT_NAME).toBundle();
115             getActivity().startActivity(intent, bundle);
116         }
117     }
118 }
119