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