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