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