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 androidx.leanback.widget; 15 16 import android.content.Context; 17 import android.content.res.TypedArray; 18 import android.util.AttributeSet; 19 import android.util.TypedValue; 20 21 import androidx.leanback.R; 22 import androidx.recyclerview.widget.RecyclerView; 23 24 /** 25 * A {@link android.view.ViewGroup} that shows items in a vertically scrolling list. The items 26 * come from the {@link RecyclerView.Adapter} associated with this view. 27 * <p> 28 * {@link RecyclerView.Adapter} can optionally implement {@link FacetProviderAdapter} which 29 * provides {@link FacetProvider} for a given view type; {@link RecyclerView.ViewHolder} 30 * can also implement {@link FacetProvider}. Facet from ViewHolder 31 * has a higher priority than the one from FacetProviderAdapter associated with viewType. 32 * Supported optional facets are: 33 * <ol> 34 * <li> {@link ItemAlignmentFacet} 35 * When this facet is provided by ViewHolder or FacetProviderAdapter, it will 36 * override the item alignment settings set on VerticalGridView. This facet also allows multiple 37 * alignment positions within one ViewHolder. 38 * </li> 39 * </ol> 40 */ 41 public class VerticalGridView extends BaseGridView { 42 VerticalGridView(Context context)43 public VerticalGridView(Context context) { 44 this(context, null); 45 } 46 VerticalGridView(Context context, AttributeSet attrs)47 public VerticalGridView(Context context, AttributeSet attrs) { 48 this(context, attrs, 0); 49 } 50 VerticalGridView(Context context, AttributeSet attrs, int defStyle)51 public VerticalGridView(Context context, AttributeSet attrs, int defStyle) { 52 super(context, attrs, defStyle); 53 mLayoutManager.setOrientation(RecyclerView.VERTICAL); 54 initAttributes(context, attrs); 55 } 56 initAttributes(Context context, AttributeSet attrs)57 protected void initAttributes(Context context, AttributeSet attrs) { 58 initBaseGridViewAttributes(context, attrs); 59 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbVerticalGridView); 60 setColumnWidth(a); 61 setNumColumns(a.getInt(R.styleable.lbVerticalGridView_numberOfColumns, 1)); 62 a.recycle(); 63 } 64 setColumnWidth(TypedArray array)65 void setColumnWidth(TypedArray array) { 66 TypedValue typedValue = array.peekValue(R.styleable.lbVerticalGridView_columnWidth); 67 if (typedValue != null) { 68 int size = array.getLayoutDimension(R.styleable.lbVerticalGridView_columnWidth, 0); 69 setColumnWidth(size); 70 } 71 } 72 73 /** 74 * Sets the number of columns. Defaults to one. 75 */ setNumColumns(int numColumns)76 public void setNumColumns(int numColumns) { 77 mLayoutManager.setNumRows(numColumns); 78 requestLayout(); 79 } 80 81 /** 82 * Sets the column width. 83 * 84 * @param width May be {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, or a size 85 * in pixels. If zero, column width will be fixed based on number of columns 86 * and view width. 87 */ setColumnWidth(int width)88 public void setColumnWidth(int width) { 89 mLayoutManager.setRowHeight(width); 90 requestLayout(); 91 } 92 } 93