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/**
24a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * A {@link android.view.ViewGroup} that shows items in a vertically scrolling list. The items
25a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * come from the {@link RecyclerView.Adapter} associated with this view.
2608c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * <p>
2708c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * {@link RecyclerView.Adapter} can optionally implement {@link FacetProviderAdapter} which
2808c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * provides {@link FacetProvider} for a given view type;  {@link RecyclerView.ViewHolder}
2908c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * can also implement {@link FacetProvider}.  Facet from ViewHolder
3008c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * has a higher priority than the one from FacetProiderAdapter associated with viewType.
3108c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * Supported optional facets are:
3208c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * <ol>
3308c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * <li> {@link ItemAlignmentFacet}
3408c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * When this facet is provided by ViewHolder or FacetProviderAdapter,  it will
3508c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * override the item alignment settings set on VerticalGridView.  This facet also allows multiple
3608c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * alignment positions within one ViewHolder.
3708c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * </li>
3808c56822b71ab0aa0b9bb03e5fd45e28f6e358b8Dake Gu * </ol>
399106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn */
40a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbournpublic class VerticalGridView extends BaseGridView {
419106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn
42a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn    public VerticalGridView(Context context) {
439106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn        this(context, null);
449106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn    }
459106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn
46a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn    public VerticalGridView(Context context, AttributeSet attrs) {
479106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn        this(context, attrs, 0);
489106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn    }
499106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn
50a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn    public VerticalGridView(Context context, AttributeSet attrs, int defStyle) {
519106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn        super(context, attrs, defStyle);
529106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn        mLayoutManager.setOrientation(RecyclerView.VERTICAL);
53cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        initAttributes(context, attrs);
54cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    }
55cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn
56cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    protected void initAttributes(Context context, AttributeSet attrs) {
57a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn        initBaseGridViewAttributes(context, attrs);
58cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbVerticalGridView);
59f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout        setColumnWidth(a);
60cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        setNumColumns(a.getInt(R.styleable.lbVerticalGridView_numberOfColumns, 1));
61cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        a.recycle();
62cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    }
63cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn
64f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout    void setColumnWidth(TypedArray array) {
65f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout        TypedValue typedValue = array.peekValue(R.styleable.lbVerticalGridView_columnWidth);
6646443cb5b092f1d9156342645088eead9da026f6Dake Gu        if (typedValue != null) {
6746443cb5b092f1d9156342645088eead9da026f6Dake Gu            int size = array.getLayoutDimension(R.styleable.lbVerticalGridView_columnWidth, 0);
6846443cb5b092f1d9156342645088eead9da026f6Dake Gu            setColumnWidth(size);
69f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout        }
70f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout    }
71f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout
72cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    /**
73a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     * Sets the number of columns.  Defaults to one.
74cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn     */
75cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    public void setNumColumns(int numColumns) {
76cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        mLayoutManager.setNumRows(numColumns);
77cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        requestLayout();
78cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    }
79cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn
80cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    /**
81a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     * Sets the column width.
82f272f7533fcb5aba341e9ab2f4ff0421d668a8caCraig Stout     *
83a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     * @param width May be {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, or a size
84a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     *              in pixels. If zero, column width will be fixed based on number of columns
85a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     *              and view width.
86cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn     */
87cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn    public void setColumnWidth(int width) {
88cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        mLayoutManager.setRowHeight(width);
89cfbb302b7a67d1633b1a68f659ed41aa71702507Tim Kilbourn        requestLayout();
909106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn    }
919106804a84e5e8733e0b9313f749fa1f726e5d11Tim Kilbourn}
92