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.os.Handler; 19 import android.util.Log; 20 import android.view.View; 21 22 import androidx.core.content.res.ResourcesCompat; 23 import androidx.leanback.widget.ArrayObjectAdapter; 24 import androidx.leanback.widget.HeaderItem; 25 import androidx.leanback.widget.ListRow; 26 import androidx.leanback.widget.ListRowPresenter; 27 import androidx.leanback.widget.OnItemViewClickedListener; 28 import androidx.leanback.widget.Presenter; 29 import androidx.leanback.widget.Row; 30 import androidx.leanback.widget.RowPresenter; 31 32 import java.util.Random; 33 34 public class BrowseAnimationFragment extends 35 androidx.leanback.app.BrowseFragment { 36 private static final String TAG = "leanback.BrowseAnimationFragment"; 37 38 private static final int NUM_ROWS = 10; 39 private ArrayObjectAdapter mRowsAdapter; 40 private static Random sRand = new Random(); 41 42 static class Item { 43 final String mText; 44 final OnItemViewClickedListener mAction; 45 Item(String text, OnItemViewClickedListener action)46 Item(String text, OnItemViewClickedListener action) { 47 mText = text; 48 mAction = action; 49 } 50 51 @Override toString()52 public String toString() { 53 return mText; 54 } 55 } 56 57 @Override onCreate(Bundle savedInstanceState)58 public void onCreate(Bundle savedInstanceState) { 59 Log.i(TAG, "onCreate"); 60 super.onCreate(savedInstanceState); 61 62 setBadgeDrawable(ResourcesCompat.getDrawable(getActivity().getResources(), 63 R.drawable.ic_title, getActivity().getTheme())); 64 setTitle("Leanback Sample App"); 65 setHeadersState(HEADERS_ENABLED); 66 67 setOnSearchClickedListener(new View.OnClickListener() { 68 @Override 69 public void onClick(View view) { 70 Intent intent = new Intent(getActivity(), SearchActivity.class); 71 startActivity(intent); 72 } 73 }); 74 75 setupRows(); 76 setOnItemViewClickedListener(new ItemViewClickedListener()); 77 } 78 setupRows()79 private void setupRows() { 80 ListRowPresenter lrp = new ListRowPresenter(); 81 mRowsAdapter = new ArrayObjectAdapter(lrp); 82 for (int i = 0; i < NUM_ROWS; ++i) { 83 mRowsAdapter.add( 84 createRandomRow(new HeaderItem(i, "Row " + i))); 85 } 86 setAdapter(mRowsAdapter); 87 } 88 createRandomItem()89 Item createRandomItem() { 90 switch (sRand.nextInt(15)) { 91 default: 92 case 0: 93 return new Item("Remove Item before", new OnItemViewClickedListener() { 94 @Override 95 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 96 RowPresenter.ViewHolder rowViewHolder, Row row) { 97 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 98 .getAdapter()); 99 int index = adapter.indexOf(item); 100 if (index >= 0) { 101 if (index > 0) 102 index--; 103 adapter.removeItems(index, 1); 104 } 105 } 106 }); 107 case 1: 108 return new Item("Remove Item after", new OnItemViewClickedListener() { 109 @Override 110 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 111 RowPresenter.ViewHolder rowViewHolder, Row row) { 112 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 113 .getAdapter()); 114 int index = adapter.indexOf(item); 115 if (index >= 0) { 116 if (index < adapter.size() - 1) 117 index++; 118 adapter.removeItems(index, 1); 119 } 120 } 121 }); 122 case 2: 123 return new Item("Remove Item", new OnItemViewClickedListener() { 124 @Override 125 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 126 RowPresenter.ViewHolder rowViewHolder, Row row) { 127 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 128 .getAdapter()); 129 int index = adapter.indexOf(item); 130 if (index >= 0) { 131 adapter.removeItems(index, 1); 132 } 133 } 134 }); 135 case 3: 136 return new Item("Remove all Items", new OnItemViewClickedListener() { 137 @Override 138 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 139 RowPresenter.ViewHolder rowViewHolder, Row row) { 140 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 141 .getAdapter()); 142 adapter.clear(); 143 } 144 }); 145 case 4: 146 return new Item("add item before", new OnItemViewClickedListener() { 147 @Override 148 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 149 RowPresenter.ViewHolder rowViewHolder, Row row) { 150 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 151 .getAdapter()); 152 int index = adapter.indexOf(item); 153 if (index >= 0) { 154 adapter.add(index, createRandomItem()); 155 } 156 } 157 }); 158 case 5: 159 return new Item("add item after", new OnItemViewClickedListener() { 160 @Override 161 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 162 RowPresenter.ViewHolder rowViewHolder, Row row) { 163 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 164 .getAdapter()); 165 int index = adapter.indexOf(item); 166 if (index >= 0) { 167 adapter.add(index + 1, createRandomItem()); 168 } 169 } 170 }); 171 case 6: 172 return new Item("add random items before", 173 new OnItemViewClickedListener() { 174 @Override 175 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 176 RowPresenter.ViewHolder rowViewHolder, Row row) { 177 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 178 .getAdapter()); 179 int index = adapter.indexOf(item); 180 if (index >= 0) { 181 int count = sRand.nextInt(4) + 1; 182 for (int i = 0; i < count; i++) { 183 adapter.add(index + i, createRandomItem()); 184 } 185 } 186 } 187 }); 188 case 7: 189 return new Item("add random items after", 190 new OnItemViewClickedListener() { 191 @Override 192 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 193 RowPresenter.ViewHolder rowViewHolder, Row row) { 194 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 195 .getAdapter()); 196 int index = adapter.indexOf(item); 197 if (index >= 0) { 198 int count = sRand.nextInt(4) + 1; 199 for (int i = 0; i < count; i++) { 200 adapter.add(index + 1 + i, 201 createRandomItem()); 202 } 203 } 204 } 205 }); 206 case 8: 207 return new Item("add row before", new OnItemViewClickedListener() { 208 @Override 209 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 210 RowPresenter.ViewHolder rowViewHolder, Row row) { 211 int index = mRowsAdapter.indexOf(row); 212 if (index >= 0) { 213 int headerId = sRand.nextInt(); 214 mRowsAdapter.add(index, createRandomRow(new HeaderItem( 215 headerId, "Row " + headerId))); 216 } 217 } 218 }); 219 case 9: 220 return new Item("add row after", new OnItemViewClickedListener() { 221 @Override 222 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 223 RowPresenter.ViewHolder rowViewHolder, Row row) { 224 int index = mRowsAdapter.indexOf(row); 225 if (index >= 0) { 226 int headerId = sRand.nextInt(); 227 mRowsAdapter.add( 228 index + 1, createRandomRow(new HeaderItem( 229 headerId, "Row " + headerId))); 230 } 231 } 232 }); 233 case 10: 234 return new Item("delete row", new OnItemViewClickedListener() { 235 @Override 236 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 237 RowPresenter.ViewHolder rowViewHolder, Row row) { 238 mRowsAdapter.remove(row); 239 } 240 }); 241 case 11: 242 return new Item("delete row before", new OnItemViewClickedListener() { 243 @Override 244 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 245 RowPresenter.ViewHolder rowViewHolder, Row row) { 246 int index = mRowsAdapter.indexOf(row); 247 if (index > 0) { 248 mRowsAdapter.removeItems(index - 1, 1); 249 } 250 } 251 }); 252 case 12: 253 return new Item("delete row after", new OnItemViewClickedListener() { 254 @Override 255 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 256 RowPresenter.ViewHolder rowViewHolder, Row row) { 257 int index = mRowsAdapter.indexOf(row); 258 if (index < mRowsAdapter.size() - 1) { 259 mRowsAdapter.removeItems(index + 1, 1); 260 } 261 } 262 }); 263 case 13: 264 return new Item("Replace Item before", new OnItemViewClickedListener() { 265 @Override 266 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 267 RowPresenter.ViewHolder rowViewHolder, Row row) { 268 ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 269 .getAdapter()); 270 int index = adapter.indexOf(item); 271 if (index >= 0) { 272 if (index > 0) 273 index--; 274 adapter.replace(index, createRandomItem()); 275 } 276 } 277 }); 278 case 14: 279 return new Item("Remove all then re-add", new OnItemViewClickedListener() { 280 @Override 281 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 282 RowPresenter.ViewHolder rowViewHolder, Row row) { 283 final ArrayObjectAdapter adapter = ((ArrayObjectAdapter) ((ListRow) row) 284 .getAdapter()); 285 adapter.clear(); 286 new Handler().postDelayed(new Runnable() { 287 @Override 288 public void run() { 289 adapter.add(0, createRandomItem()); 290 } 291 }, 1000); 292 } 293 }); 294 } 295 } 296 297 ListRow createRandomRow(HeaderItem header) { 298 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter( 299 new StringPresenter()); 300 for (int i = 0; i < 8; i++) { 301 listRowAdapter.add(createRandomItem()); 302 } 303 return new ListRow(header, listRowAdapter); 304 } 305 306 private final class ItemViewClickedListener implements OnItemViewClickedListener { 307 @Override 308 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 309 RowPresenter.ViewHolder rowViewHolder, Row row) { 310 ((Item) item).mAction.onItemClicked(itemViewHolder, item, rowViewHolder, row); 311 } 312 } 313 } 314