19106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn/* 29106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * Copyright (C) 2014 The Android Open Source Project 39106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * 49106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 59106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * in compliance with the License. You may obtain a copy of the License at 69106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * 79106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * http://www.apache.org/licenses/LICENSE-2.0 89106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * 99106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * Unless required by applicable law or agreed to in writing, software distributed under the License 109106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 119106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * or implied. See the License for the specific language governing permissions and limitations under 129106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * the License. 139106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn */ 149106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbournpackage android.support.v17.leanback.widget; 159106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn 169106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbournimport android.content.Context; 17cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbournimport android.content.res.TypedArray; 18cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbournimport android.support.v17.leanback.R; 199106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbournimport android.support.v7.widget.RecyclerView; 209106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbournimport android.util.AttributeSet; 21f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stoutimport android.util.TypedValue; 229106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn 239106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn/** 249106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * A view that shows items in a vertically scrolling list. The items come from 259106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn * the {@link RecyclerView.Adapter} associated with this view. 269106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn */ 27a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbournpublic class VerticalGridView extends BaseGridView { 289106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn 29a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn public VerticalGridView(Context context) { 309106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn this(context, null); 319106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn } 329106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn 33a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn public VerticalGridView(Context context, AttributeSet attrs) { 349106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn this(context, attrs, 0); 359106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn } 369106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn 37a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn public VerticalGridView(Context context, AttributeSet attrs, int defStyle) { 389106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn super(context, attrs, defStyle); 399106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn mLayoutManager.setOrientation(RecyclerView.VERTICAL); 40cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn initAttributes(context, attrs); 41cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn } 42cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn 43cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn protected void initAttributes(Context context, AttributeSet attrs) { 44a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn initBaseGridViewAttributes(context, attrs); 45cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbVerticalGridView); 46f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout setColumnWidth(a); 47cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn setNumColumns(a.getInt(R.styleable.lbVerticalGridView_numberOfColumns, 1)); 48cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn a.recycle(); 49cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn } 50cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn 51f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout void setColumnWidth(TypedArray array) { 52f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout TypedValue typedValue = array.peekValue(R.styleable.lbVerticalGridView_columnWidth); 53f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout int size; 54f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout if (typedValue != null && typedValue.type == TypedValue.TYPE_DIMENSION) { 55f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout size = array.getDimensionPixelSize(R.styleable.lbVerticalGridView_columnWidth, 0); 56f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout } else { 57f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout size = array.getInt(R.styleable.lbVerticalGridView_columnWidth, 0); 58f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout } 59f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout setColumnWidth(size); 60f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout } 61f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout 62cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn /** 63f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout * Set the number of columns. Defaults to one. 64cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn */ 65cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn public void setNumColumns(int numColumns) { 66cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn mLayoutManager.setNumRows(numColumns); 67cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn requestLayout(); 68cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn } 69cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn 70cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn /** 71cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn * Set the column width. 72f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout * 73f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout * @param width May be WRAP_CONTENT, or a size in pixels. If zero, 74f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout * column width will be fixed based on number of columns and view width. 75cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn */ 76cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn public void setColumnWidth(int width) { 77cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn mLayoutManager.setRowHeight(width); 78cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn requestLayout(); 799106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn } 809106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn} 81