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