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.Context; 17 import android.content.Intent; 18 import android.os.Bundle; 19 import android.os.Handler; 20 import android.util.Log; 21 import android.view.View; 22 23 import androidx.core.content.res.ResourcesCompat; 24 import androidx.leanback.widget.ArrayObjectAdapter; 25 import androidx.leanback.widget.OnItemViewClickedListener; 26 import androidx.leanback.widget.OnItemViewSelectedListener; 27 import androidx.leanback.widget.Presenter; 28 import androidx.leanback.widget.Row; 29 import androidx.leanback.widget.RowPresenter; 30 import androidx.leanback.widget.VerticalGridPresenter; 31 32 public class VerticalGridFragment extends androidx.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 final Context context = getActivity(); 56 setBadgeDrawable(ResourcesCompat.getDrawable(context.getResources(), 57 R.drawable.ic_title, context.getTheme())); 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 @Override 70 public void run() { 71 loadData(); 72 startEntranceTransition(); 73 } 74 }, 2000); 75 } 76 loadData()77 private void loadData() { 78 for (int i = 0; i < NUM_ITEMS; i++) { 79 mAdapter.add(Integer.toString(i)); 80 } 81 } 82 setupFragment()83 private void setupFragment() { 84 VerticalGridPresenter gridPresenter = new VerticalGridPresenter(); 85 gridPresenter.setNumberOfColumns(NUM_COLUMNS); 86 setGridPresenter(gridPresenter); 87 88 mAdapter = new Adapter(new StringPresenter()); 89 setAdapter(mAdapter); 90 91 setOnItemViewSelectedListener(new OnItemViewSelectedListener() { 92 @Override 93 public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 94 RowPresenter.ViewHolder rowViewHolder, Row row) { 95 Log.i(TAG, "onItemSelected: " + item + " row " + row); 96 } 97 }); 98 99 setOnItemViewClickedListener(new OnItemViewClickedListener() { 100 @Override 101 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 102 RowPresenter.ViewHolder rowViewHolder, Row row) { 103 Log.i(TAG, "onItemClicked: " + item + " row " + row); 104 mAdapter.callNotifyChanged(); 105 } 106 }); 107 setOnSearchClickedListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View view) { 110 Intent intent = new Intent(getActivity(), SearchActivity.class); 111 startActivity(intent); 112 } 113 }); 114 } 115 } 116