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