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