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.v17.leanback.widget.ArrayObjectAdapter; 20 import android.support.v17.leanback.widget.Presenter; 21 import android.support.v17.leanback.widget.RowPresenter; 22 import android.support.v17.leanback.widget.VerticalGridPresenter; 23 import android.support.v17.leanback.widget.Row; 24 import android.support.v17.leanback.widget.OnItemViewClickedListener; 25 import android.support.v17.leanback.widget.OnItemViewSelectedListener; 26 import android.util.Log; 27 import android.view.Gravity; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.widget.TextView; 31 32 public class VerticalGridFragment extends android.support.v17.leanback.app.VerticalGridFragment { 33 private static final String TAG = "leanback.VerticalGridFragment"; 34 35 private static final int NUM_COLUMNS = 3; 36 private static final int NUM_ITEMS = 50; 37 private static final int HEIGHT = 200; 38 private static final boolean TEST_ENTRANCE_TRANSITION = true; 39 40 private static class Adapter extends ArrayObjectAdapter { Adapter(StringPresenter presenter)41 public Adapter(StringPresenter presenter) { 42 super(presenter); 43 } callNotifyChanged()44 public void callNotifyChanged() { 45 super.notifyChanged(); 46 } 47 } 48 private Adapter mAdapter; 49 50 @Override onCreate(Bundle savedInstanceState)51 public void onCreate(Bundle savedInstanceState) { 52 Log.i(TAG, "onCreate"); 53 super.onCreate(savedInstanceState); 54 55 setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title)); 56 setTitle("Leanback Vertical Grid Demo"); 57 58 setupFragment(); 59 if (TEST_ENTRANCE_TRANSITION) { 60 // don't run entrance transition if fragment is restored. 61 if (savedInstanceState == null) { 62 prepareEntranceTransition(); 63 } 64 } 65 // simulates in a real world use case data being loaded two seconds later 66 new Handler().postDelayed(new Runnable() { 67 public void run() { 68 loadData(); 69 startEntranceTransition(); 70 } 71 }, 2000); 72 } 73 loadData()74 private void loadData() { 75 for (int i = 0; i < NUM_ITEMS; i++) { 76 mAdapter.add(Integer.toString(i)); 77 } 78 } 79 setupFragment()80 private void setupFragment() { 81 VerticalGridPresenter gridPresenter = new VerticalGridPresenter(); 82 gridPresenter.setNumberOfColumns(NUM_COLUMNS); 83 setGridPresenter(gridPresenter); 84 85 mAdapter = new Adapter(new StringPresenter()); 86 setAdapter(mAdapter); 87 88 setOnItemViewSelectedListener(new OnItemViewSelectedListener() { 89 @Override 90 public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 91 RowPresenter.ViewHolder rowViewHolder, Row row) { 92 Log.i(TAG, "onItemSelected: " + item + " row " + row); 93 } 94 }); 95 96 setOnItemViewClickedListener(new OnItemViewClickedListener() { 97 @Override 98 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 99 RowPresenter.ViewHolder rowViewHolder, Row row) { 100 Log.i(TAG, "onItemClicked: " + item + " row " + row); 101 mAdapter.callNotifyChanged(); 102 } 103 }); 104 setOnSearchClickedListener(new View.OnClickListener() { 105 @Override 106 public void onClick(View view) { 107 Intent intent = new Intent(getActivity(), SearchActivity.class); 108 startActivity(intent); 109 } 110 }); 111 } 112 } 113