• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static class Adapter extends ArrayObjectAdapter {
Adapter(StringPresenter presenter)39         public Adapter(StringPresenter presenter) {
40             super(presenter);
41         }
callNotifyChanged()42         public void callNotifyChanged() {
43             super.notifyChanged();
44         }
45     }
46     private Adapter mAdapter;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     public void onCreate(Bundle savedInstanceState) {
50         Log.i(TAG, "onCreate");
51         super.onCreate(savedInstanceState);
52 
53         setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title));
54         setTitle("Leanback Vertical Grid Demo");
55 
56         setupFragment();
57     }
58 
setupFragment()59     private void setupFragment() {
60         VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
61         gridPresenter.setNumberOfColumns(NUM_COLUMNS);
62         setGridPresenter(gridPresenter);
63 
64         mAdapter = new Adapter(new StringPresenter());
65         for (int i = 0; i < NUM_ITEMS; i++) {
66             mAdapter.add(Integer.toString(i));
67         }
68         setAdapter(mAdapter);
69 
70         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
71             @Override
72             public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
73                     RowPresenter.ViewHolder rowViewHolder, Row row) {
74                 Log.i(TAG, "onItemSelected: " + item + " row " + row);
75             }
76         });
77 
78         setOnItemViewClickedListener(new OnItemViewClickedListener() {
79             @Override
80             public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
81                     RowPresenter.ViewHolder rowViewHolder, Row row) {
82                 Log.i(TAG, "onItemClicked: " + item + " row " + row);
83                 mAdapter.callNotifyChanged();
84             }
85         });
86         setOnSearchClickedListener(new View.OnClickListener() {
87             @Override
88             public void onClick(View view) {
89                 Intent intent = new Intent(getActivity(), SearchActivity.class);
90                 startActivity(intent);
91             }
92         });
93     }
94 }
95