1 /* 2 * Copyright (C) 2013 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 17 package com.example.android.leanback; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.graphics.Color; 22 import android.os.Bundle; 23 import android.support.v17.leanback.widget.HorizontalGridView; 24 import android.support.v17.leanback.widget.OnChildViewHolderSelectedListener; 25 import android.support.v7.widget.RecyclerView; 26 import android.util.Log; 27 import android.view.View; 28 import android.view.View.OnClickListener; 29 import android.view.View.OnFocusChangeListener; 30 import android.view.ViewGroup; 31 import android.widget.TextView; 32 33 public class HorizontalGridTestActivity extends Activity { 34 private static final String TAG = "HorizontalGridTestActivity"; 35 private static final boolean DEBUG = true; 36 private static final String SELECT_ACTION = "android.test.leanback.widget.SELECT"; 37 private static final int NUM_ITEMS = 100; 38 private static final boolean STAGGERED = true; 39 40 private HorizontalGridView mHorizontalGridView; 41 42 private RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() { 43 @Override 44 public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 45 if (DEBUG) { 46 final String[] stateNames = { "IDLE", "DRAGGING", "SETTLING" }; 47 Log.v(TAG, "onScrollStateChanged " 48 + (newState < stateNames.length ? stateNames[newState] : newState)); 49 } 50 } 51 }; 52 53 private View createView() { 54 View view = getLayoutInflater().inflate(R.layout.horizontal_grid, null, false); 55 mHorizontalGridView = (HorizontalGridView) view.findViewById(R.id.gridview); 56 57 mHorizontalGridView.setWindowAlignment(HorizontalGridView.WINDOW_ALIGN_BOTH_EDGE); 58 mHorizontalGridView.setWindowAlignmentOffsetPercent(35); 59 mHorizontalGridView.setOnChildViewHolderSelectedListener( 60 new OnChildViewHolderSelectedListener() { 61 @Override 62 public void onChildViewHolderSelected(RecyclerView parent, 63 RecyclerView.ViewHolder child, 64 int position, int subposition) { 65 if (DEBUG) Log.d(TAG, "onChildSelected position=" + position); 66 } 67 68 }); 69 return view; 70 } 71 72 @Override 73 protected void onCreate(Bundle savedInstanceState) { 74 super.onCreate(savedInstanceState); 75 76 if (DEBUG) Log.v(TAG, "onCreate"); 77 78 RecyclerView.Adapter adapter = new MyAdapter(); 79 80 View view = createView(); 81 82 mHorizontalGridView.setAdapter(new MyAdapter()); 83 setContentView(view); 84 85 mHorizontalGridView.addOnScrollListener(mScrollListener); 86 } 87 88 @Override 89 protected void onNewIntent(Intent intent) { 90 if (DEBUG) Log.v(TAG, "onNewIntent " + intent); 91 if (intent.getAction().equals(SELECT_ACTION)) { 92 int position = intent.getIntExtra("SELECT_POSITION", -1); 93 if (position >= 0) { 94 mHorizontalGridView.setSelectedPosition(position); 95 } 96 } 97 super.onNewIntent(intent); 98 } 99 100 private OnFocusChangeListener mItemFocusChangeListener = new OnFocusChangeListener() { 101 102 @Override 103 public void onFocusChange(View v, boolean hasFocus) { 104 if (hasFocus) { 105 v.setBackgroundColor(Color.YELLOW); 106 } else { 107 v.setBackgroundColor(Color.LTGRAY); 108 } 109 } 110 }; 111 112 private OnClickListener mItemClickListener = new OnClickListener() { 113 @Override 114 public void onClick(View v) { 115 mHorizontalGridView.getAdapter().notifyDataSetChanged(); 116 } 117 }; 118 119 class MyAdapter extends RecyclerView.Adapter { 120 121 private int[] mItemLengths; 122 123 MyAdapter() { 124 mItemLengths = new int[NUM_ITEMS]; 125 for (int i = 0; i < mItemLengths.length; i++) { 126 mItemLengths[i] = STAGGERED ? (int)(Math.random() * 180) + 180 : 240; 127 } 128 } 129 130 @Override 131 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 132 if (DEBUG) Log.v(TAG, "createViewHolder " + viewType); 133 TextView textView = new TextView(parent.getContext()); 134 textView.setTextColor(Color.BLACK); 135 textView.setFocusable(true); 136 textView.setFocusableInTouchMode(true); 137 textView.setOnFocusChangeListener(mItemFocusChangeListener); 138 textView.setOnClickListener(mItemClickListener); 139 return new ViewHolder(textView); 140 } 141 142 @Override 143 public void onBindViewHolder(RecyclerView.ViewHolder baseHolder, int position) { 144 if (DEBUG) Log.v(TAG, "bindViewHolder " + position + " " + baseHolder); 145 ViewHolder holder = (ViewHolder) baseHolder; 146 ((TextView) holder.itemView).setText("Item "+position); 147 holder.itemView.setBackgroundColor(Color.LTGRAY); 148 holder.itemView.setLayoutParams(new ViewGroup.MarginLayoutParams(mItemLengths[position], 149 80)); 150 } 151 152 @Override 153 public int getItemCount() { 154 return mItemLengths.length; 155 } 156 } 157 158 static class ViewHolder extends RecyclerView.ViewHolder { 159 160 public ViewHolder(View v) { 161 super(v); 162 } 163 } 164 } 165