1 /* This file is auto-generated from VerticalGridFragment.  DO NOT MODIFY. */
2 
3 /*
4  * Copyright (C) 2014 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.example.android.leanback;
17 
18 import android.content.Intent;
19 import android.os.Bundle;
20 import android.os.Handler;
21 import android.support.v17.leanback.widget.ArrayObjectAdapter;
22 import android.support.v17.leanback.widget.Presenter;
23 import android.support.v17.leanback.widget.RowPresenter;
24 import android.support.v17.leanback.widget.VerticalGridPresenter;
25 import android.support.v17.leanback.widget.Row;
26 import android.support.v17.leanback.widget.OnItemViewClickedListener;
27 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
28 import android.util.Log;
29 import android.view.Gravity;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.TextView;
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         setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title));
58         setTitle("Leanback Vertical Grid Demo");
59 
60         setupFragment();
61         if (TEST_ENTRANCE_TRANSITION) {
62             // don't run entrance transition if fragment is restored.
63             if (savedInstanceState == null) {
64                 prepareEntranceTransition();
65             }
66         }
67         // simulates in a real world use case  data being loaded two seconds later
68         new Handler().postDelayed(new Runnable() {
69             public void run() {
70                 loadData();
71                 startEntranceTransition();
72             }
73         }, 2000);
74     }
75 
loadData()76     private void loadData() {
77         for (int i = 0; i < NUM_ITEMS; i++) {
78             mAdapter.add(Integer.toString(i));
79         }
80     }
81 
setupFragment()82     private void setupFragment() {
83         VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
84         gridPresenter.setNumberOfColumns(NUM_COLUMNS);
85         setGridPresenter(gridPresenter);
86 
87         mAdapter = new Adapter(new StringPresenter());
88         setAdapter(mAdapter);
89 
90         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
91             @Override
92             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
93                     RowPresenter.ViewHolder rowViewHolder, Row row) {
94                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
95             }
96         });
97 
98         setOnItemViewClickedListener(new OnItemViewClickedListener() {
99             @Override
100             public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
101                     RowPresenter.ViewHolder rowViewHolder, Row row) {
102                 Log.i(TAG, "onItemClicked: " + item + " row " + row);
103                 mAdapter.callNotifyChanged();
104             }
105         });
106         setOnSearchClickedListener(new View.OnClickListener() {
107             @Override
108             public void onClick(View view) {
109                 Intent intent = new Intent(getActivity(), SearchSupportActivity.class);
110                 startActivity(intent);
111             }
112         });
113     }
114 }
115