• 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.Intent;
17 import android.os.Bundle;
18 import android.os.Handler;
19 import android.support.v4.app.ActivityOptionsCompat;
20 import android.support.v17.leanback.widget.ArrayObjectAdapter;
21 import android.support.v17.leanback.widget.HeaderItem;
22 import android.support.v17.leanback.widget.ImageCardView;
23 import android.support.v17.leanback.widget.ListRow;
24 import android.support.v17.leanback.widget.ListRowPresenter;
25 import android.support.v17.leanback.widget.OnItemViewClickedListener;
26 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
27 import android.support.v17.leanback.widget.Presenter;
28 import android.support.v17.leanback.widget.Row;
29 import android.support.v17.leanback.widget.RowPresenter;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.ViewGroup;
33 
34 public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragment {
35     private static final String TAG = "leanback.BrowseFragment";
36 
37     private static final boolean TEST_ENTRANCE_TRANSITION = true;
38     private static final int NUM_ROWS = 10;
39     private ArrayObjectAdapter mRowsAdapter;
40 
41     @Override
onCreate(Bundle savedInstanceState)42     public void onCreate(Bundle savedInstanceState) {
43         Log.i(TAG, "onCreate");
44         super.onCreate(savedInstanceState);
45 
46         setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title));
47         setTitle("Leanback Sample App");
48         setHeadersState(HEADERS_ENABLED);
49 
50         setOnSearchClickedListener(new View.OnClickListener() {
51             @Override
52             public void onClick(View view) {
53                 Intent intent = new Intent(getActivity(), SearchActivity.class);
54                 startActivity(intent);
55             }
56         });
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             }
66         });
67         if (TEST_ENTRANCE_TRANSITION) {
68             // don't run entrance transition if Activity is restored.
69             if (savedInstanceState == null) {
70                 prepareEntranceTransition();
71             }
72             // simulate delay loading data
73             new Handler().postDelayed(new Runnable() {
74                 public void run() {
75                     startEntranceTransition();
76                 }
77             }, 2000);
78         }
79     }
80 
setupRows()81     private void setupRows() {
82         ListRowPresenter lrp = new ListRowPresenter();
83         lrp.setRowHeight(CardPresenter.getRowHeight(getActivity()));
84         lrp.setExpandedRowHeight(CardPresenter.getExpandedRowHeight(getActivity()));
85 
86         mRowsAdapter = new ArrayObjectAdapter(lrp);
87 
88         // For good performance, it's important to use a single instance of
89         // a card presenter for all rows using that presenter.
90         final CardPresenter cardPresenter = new CardPresenter();
91 
92         for (int i = 0; i < NUM_ROWS; ++i) {
93             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
94             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
95             listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_2));
96             listRowAdapter.add(new PhotoItem("Android TV", "by Google", R.drawable.gallery_photo_3));
97             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
98             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5));
99             listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_6));
100             listRowAdapter.add(new PhotoItem("Android TV", "open RowsActivity", R.drawable.gallery_photo_7));
101             listRowAdapter.add(new PhotoItem("Leanback", "open MainActivity", R.drawable.gallery_photo_8));
102             HeaderItem header = new HeaderItem(i, "Row " + i);
103             mRowsAdapter.add(new ListRow(header, listRowAdapter));
104         }
105 
106         setAdapter(mRowsAdapter);
107     }
108 
109     private final class ItemViewClickedListener implements OnItemViewClickedListener {
110         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)111         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
112                 RowPresenter.ViewHolder rowViewHolder, Row row) {
113 
114             Intent intent;
115             Bundle bundle;
116             if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) {
117                 intent = new Intent(getActivity(), MainActivity.class);
118                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
119                         .toBundle();
120             } else if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) {
121                 intent = new Intent(getActivity(), RowsActivity.class);
122                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
123                         .toBundle();
124             } else {
125                 intent = new Intent(getActivity(), DetailsActivity.class);
126                 intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);
127                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
128                         getActivity(),
129                         ((ImageCardView)itemViewHolder.view).getMainImageView(),
130                         DetailsActivity.SHARED_ELEMENT_NAME).toBundle();
131             }
132             getActivity().startActivity(intent, bundle);
133         }
134     }
135 }
136