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.support.v17.leanback.widget.ArrayObjectAdapter; 19 import android.support.v17.leanback.widget.Presenter; 20 import android.support.v17.leanback.widget.RowPresenter; 21 import android.support.v17.leanback.widget.VerticalGridPresenter; 22 import android.support.v17.leanback.widget.Row; 23 import android.support.v17.leanback.widget.OnItemViewClickedListener; 24 import android.support.v17.leanback.widget.OnItemViewSelectedListener; 25 import android.util.Log; 26 import android.view.Gravity; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.TextView; 30 31 public class VerticalGridFragment extends android.support.v17.leanback.app.VerticalGridFragment { 32 private static final String TAG = "leanback.VerticalGridFragment"; 33 34 private static final int NUM_COLUMNS = 3; 35 private static final int NUM_ITEMS = 50; 36 private static final int HEIGHT = 200; 37 38 private ArrayObjectAdapter mAdapter; 39 40 @Override 41 public void onCreate(Bundle savedInstanceState) { 42 Log.i(TAG, "onCreate"); 43 super.onCreate(savedInstanceState); 44 45 setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title)); 46 setTitle("Leanback Vertical Grid Demo"); 47 48 setupFragment(); 49 } 50 51 private void setupFragment() { 52 VerticalGridPresenter gridPresenter = new VerticalGridPresenter(); 53 gridPresenter.setNumberOfColumns(NUM_COLUMNS); 54 setGridPresenter(gridPresenter); 55 56 mAdapter = new ArrayObjectAdapter(new StringPresenter()); 57 for (int i = 0; i < NUM_ITEMS; i++) { 58 mAdapter.add(Integer.toString(i)); 59 } 60 setAdapter(mAdapter); 61 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 } 68 }); 69 70 setOnItemViewClickedListener(new OnItemViewClickedListener() { 71 @Override 72 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 73 RowPresenter.ViewHolder rowViewHolder, Row row) { 74 Log.i(TAG, "onItemClicked: " + item + " row " + row); 75 } 76 }); 77 setOnSearchClickedListener(new View.OnClickListener() { 78 @Override 79 public void onClick(View view) { 80 Intent intent = new Intent(getActivity(), SearchActivity.class); 81 startActivity(intent); 82 } 83 }); 84 } 85 } 86