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