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