GridActivity.java revision 85df3117f0fcd0aa10d7bd45194dab97e22112f2
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v17.leanback.widget; 18 19import android.support.v17.leanback.tests.R; 20import android.support.v7.widget.RecyclerView; 21import android.support.v17.leanback.widget.BaseGridView; 22import android.support.v17.leanback.widget.OnChildSelectedListener; 23import android.app.Activity; 24import android.content.Context; 25import android.content.Intent; 26import android.graphics.Color; 27import android.os.Bundle; 28import android.util.Log; 29import android.util.SparseArray; 30import android.view.View; 31import android.view.View.OnClickListener; 32import android.view.View.OnFocusChangeListener; 33import android.view.ViewGroup; 34import android.widget.ImageView; 35import android.widget.TextView; 36 37/** 38 * @hide from javadoc 39 */ 40public class GridActivity extends Activity { 41 private static final String TAG = "GridActivity"; 42 43 public static final String EXTRA_GRIDVIEW_LAYOUT_SIZE = "gridViewSize"; 44 public static final String EXTRA_ORIENTATION = "orientation"; 45 public static final String EXTRA_NUM_ITEMS = "numItems"; 46 public static final String EXTRA_ITEMS = "items"; 47 public static final String EXTRA_ROWS = "rows"; 48 public static final String EXTRA_STAGGERED = "staggered"; 49 public static final String SELECT_ACTION = "android.test.leanback.widget.SELECT"; 50 51 static final int DEFAULT_ORIENTATION = BaseGridView.HORIZONTAL; 52 static final int DEFAULT_NUM_ITEMS = 100; 53 static final int DEFAULT_ROWS = 3; 54 static final boolean DEFAULT_STAGGERED = true; 55 56 private static final boolean DEBUG = false; 57 58 int mOrientation; 59 int mRows; 60 int mNumItems; 61 boolean mStaggered; 62 63 int[] mGridViewLayoutSize; 64 BaseGridView mGridView; 65 int[] mItemLengths; 66 67 private int mBoundCount; 68 69 private View createView() { 70 71 View view = getLayoutInflater().inflate(mOrientation == BaseGridView.HORIZONTAL ? 72 R.layout.horizontal_grid: R.layout.vertical_grid, null, false); 73 mGridView = (BaseGridView) view.findViewById(R.id.gridview); 74 if (mGridViewLayoutSize != null) { 75 ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mGridView.getLayoutParams(); 76 lp.width = mGridViewLayoutSize[0]; 77 lp.height = mGridViewLayoutSize[1]; 78 mGridView.setLayoutParams(lp); 79 } 80 81 if (mOrientation == BaseGridView.HORIZONTAL) { 82 ((HorizontalGridView) mGridView).setNumRows(mRows); 83 } else { 84 ((VerticalGridView) mGridView).setNumColumns(mRows); 85 } 86 mGridView.setWindowAlignment(BaseGridView.WINDOW_ALIGN_BOTH_EDGE); 87 mGridView.setWindowAlignmentOffsetPercent(35); 88 mGridView.setOnChildSelectedListener(new OnChildSelectedListener() { 89 @Override 90 public void onChildSelected(ViewGroup parent, View view, int position, long id) { 91 if (DEBUG) Log.d(TAG, "onChildSelected position=" + position + " id="+id); 92 } 93 }); 94 return view; 95 } 96 97 @Override 98 protected void onCreate(Bundle savedInstanceState) { 99 Intent intent = getIntent(); 100 101 mGridViewLayoutSize = intent.getIntArrayExtra(EXTRA_GRIDVIEW_LAYOUT_SIZE); 102 mOrientation = intent.getIntExtra(EXTRA_ORIENTATION, DEFAULT_ORIENTATION); 103 mRows = intent.getIntExtra(EXTRA_ROWS, DEFAULT_ROWS); 104 mStaggered = intent.getBooleanExtra(EXTRA_STAGGERED, DEFAULT_STAGGERED); 105 mItemLengths = intent.getIntArrayExtra(EXTRA_ITEMS); 106 if (mItemLengths == null) { 107 mNumItems = intent.getIntExtra(EXTRA_NUM_ITEMS, DEFAULT_NUM_ITEMS); 108 mItemLengths = new int[mNumItems]; 109 for (int i = 0; i < mItemLengths.length; i++) { 110 if (mOrientation == BaseGridView.HORIZONTAL) { 111 mItemLengths[i] = mStaggered ? (int)(Math.random() * 180) + 180 : 240; 112 } else { 113 mItemLengths[i] = mStaggered ? (int)(Math.random() * 120) + 120 : 160; 114 } 115 } 116 } else { 117 mNumItems = mItemLengths.length; 118 } 119 120 super.onCreate(savedInstanceState); 121 122 if (DEBUG) Log.v(TAG, "onCreate " + this); 123 124 RecyclerView.Adapter adapter = new MyAdapter(); 125 126 View view = createView(); 127 128 mGridView.setAdapter(new MyAdapter()); 129 setContentView(view); 130 } 131 132 @Override 133 protected void onNewIntent(Intent intent) { 134 if (DEBUG) Log.v(TAG, "onNewIntent " + intent+ " "+this); 135 if (intent.getAction().equals(SELECT_ACTION)) { 136 int position = intent.getIntExtra("SELECT_POSITION", -1); 137 if (position >= 0) { 138 mGridView.setSelectedPosition(position); 139 } 140 } 141 super.onNewIntent(intent); 142 } 143 144 private OnFocusChangeListener mItemFocusChangeListener = new OnFocusChangeListener() { 145 146 @Override 147 public void onFocusChange(View v, boolean hasFocus) { 148 if (hasFocus) { 149 v.setBackgroundColor(Color.YELLOW); 150 } else { 151 v.setBackgroundColor(Color.LTGRAY); 152 } 153 } 154 }; 155 156 void resetBoundCount() { 157 mBoundCount = 0; 158 } 159 160 int getBoundCount() { 161 return mBoundCount; 162 } 163 164 void swap(int index1, int index2) { 165 if (index1 == index2) { 166 return; 167 } else if (index1 > index2) { 168 int index = index1; 169 index1 = index2; 170 index2 = index; 171 } 172 int value = mItemLengths[index1]; 173 mItemLengths[index1] = mItemLengths[index2]; 174 mItemLengths[index2] = value; 175 mGridView.getAdapter().notifyItemMoved(index1, index2); 176 mGridView.getAdapter().notifyItemMoved(index2 - 1, index1); 177 } 178 179 void changeArraySize(int length) { 180 mNumItems = length; 181 mGridView.getAdapter().notifyDataSetChanged(); 182 } 183 184 class MyAdapter extends RecyclerView.Adapter { 185 186 @Override 187 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 188 if (DEBUG) Log.v(TAG, "createViewHolder " + viewType); 189 TextView textView = new TextView(parent.getContext()); 190 textView.setTextColor(Color.BLACK); 191 textView.setFocusable(true); 192 textView.setFocusableInTouchMode(true); 193 textView.setOnFocusChangeListener(mItemFocusChangeListener); 194 return new ViewHolder(textView); 195 } 196 197 @Override 198 public void onBindViewHolder(RecyclerView.ViewHolder baseHolder, int position) { 199 if (DEBUG) Log.v(TAG, "bindViewHolder " + position + " " + baseHolder); 200 mBoundCount++; 201 ViewHolder holder = (ViewHolder) baseHolder; 202 ((TextView) holder.itemView).setText("Item "+position); 203 holder.itemView.setBackgroundColor(Color.LTGRAY); 204 if (mOrientation == BaseGridView.HORIZONTAL) { 205 holder.itemView.setLayoutParams(new ViewGroup.MarginLayoutParams( 206 mItemLengths[position], 80)); 207 } else { 208 holder.itemView.setLayoutParams(new ViewGroup.MarginLayoutParams( 209 240, mItemLengths[position])); 210 } 211 } 212 213 @Override 214 public int getItemCount() { 215 return mNumItems; 216 } 217 } 218 219 static class ViewHolder extends RecyclerView.ViewHolder { 220 221 public ViewHolder(View v) { 222 super(v); 223 } 224 } 225} 226