1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package android.support.v17.leanback.widget; 15 16 import android.content.Context; 17 import android.content.res.TypedArray; 18 import android.support.v17.leanback.R; 19 import android.support.v7.widget.RecyclerView; 20 import android.util.AttributeSet; 21 import android.util.TypedValue; 22 23 /** 24 * A {@link android.view.ViewGroup} that shows items in a vertically scrolling list. The items 25 * come from the {@link RecyclerView.Adapter} associated with this view. 26 * <p> 27 * {@link RecyclerView.Adapter} can optionally implement {@link FacetProviderAdapter} which 28 * provides {@link FacetProvider} for a given view type; {@link RecyclerView.ViewHolder} 29 * can also implement {@link FacetProvider}. Facet from ViewHolder 30 * has a higher priority than the one from FacetProiderAdapter associated with viewType. 31 * Supported optional facets are: 32 * <ol> 33 * <li> {@link ItemAlignmentFacet} 34 * When this facet is provided by ViewHolder or FacetProviderAdapter, it will 35 * override the item alignment settings set on VerticalGridView. This facet also allows multiple 36 * alignment positions within one ViewHolder. 37 * </li> 38 * </ol> 39 */ 40 public class VerticalGridView extends BaseGridView { 41 VerticalGridView(Context context)42 public VerticalGridView(Context context) { 43 this(context, null); 44 } 45 VerticalGridView(Context context, AttributeSet attrs)46 public VerticalGridView(Context context, AttributeSet attrs) { 47 this(context, attrs, 0); 48 } 49 VerticalGridView(Context context, AttributeSet attrs, int defStyle)50 public VerticalGridView(Context context, AttributeSet attrs, int defStyle) { 51 super(context, attrs, defStyle); 52 mLayoutManager.setOrientation(RecyclerView.VERTICAL); 53 initAttributes(context, attrs); 54 } 55 initAttributes(Context context, AttributeSet attrs)56 protected void initAttributes(Context context, AttributeSet attrs) { 57 initBaseGridViewAttributes(context, attrs); 58 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbVerticalGridView); 59 setColumnWidth(a); 60 setNumColumns(a.getInt(R.styleable.lbVerticalGridView_numberOfColumns, 1)); 61 a.recycle(); 62 } 63 setColumnWidth(TypedArray array)64 void setColumnWidth(TypedArray array) { 65 TypedValue typedValue = array.peekValue(R.styleable.lbVerticalGridView_columnWidth); 66 if (typedValue != null) { 67 int size = array.getLayoutDimension(R.styleable.lbVerticalGridView_columnWidth, 0); 68 setColumnWidth(size); 69 } 70 } 71 72 /** 73 * Sets the number of columns. Defaults to one. 74 */ setNumColumns(int numColumns)75 public void setNumColumns(int numColumns) { 76 mLayoutManager.setNumRows(numColumns); 77 requestLayout(); 78 } 79 80 /** 81 * Sets the column width. 82 * 83 * @param width May be {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, or a size 84 * in pixels. If zero, column width will be fixed based on number of columns 85 * and view width. 86 */ setColumnWidth(int width)87 public void setColumnWidth(int width) { 88 mLayoutManager.setRowHeight(width); 89 requestLayout(); 90 } 91 } 92