1 /* This file is auto-generated from RowsFragment.java.  DO NOT MODIFY. */
2 
3 /*
4  * Copyright (C) 2014 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.example.android.leanback;
17 
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.support.v17.leanback.widget.TitleHelper;
22 import android.support.v17.leanback.widget.TitleView;
23 import android.support.v4.app.ActivityOptionsCompat;
24 import android.support.v17.leanback.R;
25 import android.support.v17.leanback.widget.ArrayObjectAdapter;
26 import android.support.v17.leanback.widget.HeaderItem;
27 import android.support.v17.leanback.widget.ImageCardView;
28 import android.support.v17.leanback.widget.ListRow;
29 import android.support.v17.leanback.widget.ListRowPresenter;
30 import android.support.v17.leanback.widget.OnItemViewClickedListener;
31 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
32 import android.support.v17.leanback.widget.Presenter;
33 import android.support.v17.leanback.widget.Row;
34 import android.support.v17.leanback.widget.RowPresenter;
35 import android.util.Log;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.ViewGroup;
39 
40 public class RowsSupportFragment extends android.support.v17.leanback.app.RowsSupportFragment {
41 
42     private static final String TAG = "leanback.RowsSupportFragment";
43 
44     private static final int NUM_ROWS = 10;
45     // Row heights default to wrap content
46     private static final boolean USE_FIXED_ROW_HEIGHT = false;
47 
48     private ArrayObjectAdapter mRowsAdapter;
49     private TitleHelper mTitleHelper;
50 
setTitleHelper(TitleHelper titleHelper)51     public void setTitleHelper(TitleHelper titleHelper) {
52         mTitleHelper = titleHelper;
53     }
54 
55     @Override
onCreate(Bundle savedInstanceState)56     public void onCreate(Bundle savedInstanceState) {
57         Log.i(TAG, "onCreate");
58         super.onCreate(savedInstanceState);
59 
60         setupRows();
61         setOnItemViewClickedListener(new ItemViewClickedListener());
62         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
63             @Override
64             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
65                     RowPresenter.ViewHolder rowViewHolder, Row row) {
66                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
67                 if (mTitleHelper != null) {
68                     mTitleHelper.showTitle(getAdapter() == null || getAdapter().size() == 0 ||
69                             getAdapter().get(0) == row);
70                 }
71             }
72         });
73     }
74 
setupRows()75     private void setupRows() {
76         ListRowPresenter lrp = new ListRowPresenter();
77 
78         // For good performance, it's important to use a single instance of
79         // a card presenter for all rows using that presenter.
80         final CardPresenter cardPresenter = new CardPresenter();
81 
82         if (USE_FIXED_ROW_HEIGHT) {
83             lrp.setRowHeight(cardPresenter.getRowHeight(getActivity()));
84             lrp.setExpandedRowHeight(cardPresenter.getExpandedRowHeight(getActivity()));
85         }
86 
87         mRowsAdapter = new ArrayObjectAdapter(lrp);
88 
89         for (int i = 0; i < NUM_ROWS; ++i) {
90             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
91             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
92             listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
93             listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3));
94             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
95             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5));
96             listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_6));
97             listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_7));
98             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_8));
99             HeaderItem header = new HeaderItem(i, "Row " + i);
100             mRowsAdapter.add(new ListRow(header, listRowAdapter));
101         }
102 
103         setAdapter(mRowsAdapter);
104     }
105 
106     private final class ItemViewClickedListener implements OnItemViewClickedListener {
107         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)108         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
109                 RowPresenter.ViewHolder rowViewHolder, Row row) {
110             Intent intent = new Intent(getActivity(), DetailsSupportActivity.class);
111             intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item);
112 
113             Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
114                     getActivity(),
115                     ((ImageCardView)itemViewHolder.view).getMainImageView(),
116                     DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle();
117             getActivity().startActivity(intent, bundle);
118         }
119     }
120 }
121