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