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