HeadersFragment.java revision 729cbf4cd57c87bcd569db5974c8cbd51a942581
155c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout/*
255c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * Copyright (C) 2014 The Android Open Source Project
355c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout *
455c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * in compliance with the License. You may obtain a copy of the License at
655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout *
755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * http://www.apache.org/licenses/LICENSE-2.0
855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout *
955c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * Unless required by applicable law or agreed to in writing, software distributed under the License
1055c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1155c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * or implied. See the License for the specific language governing permissions and limitations under
1255c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * the License.
1355c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout */
1455c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
1555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutpackage android.support.v17.leanback.app;
1655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
173659dc62f9e55b1043edb4105c311c8ef997f2aeDake Guimport android.content.Context;
187a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Guimport android.graphics.Color;
199a49e322d10c1a67860d812352a5ea710843b75eDake Guimport android.graphics.drawable.ColorDrawable;
207a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Guimport android.graphics.drawable.Drawable;
217a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Guimport android.graphics.drawable.GradientDrawable;
223c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Guimport android.os.Bundle;
2355c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutimport android.support.v17.leanback.R;
2455c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutimport android.support.v17.leanback.widget.FocusHighlightHelper;
2555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutimport android.support.v17.leanback.widget.ItemBridgeAdapter;
2655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutimport android.support.v17.leanback.widget.PresenterSelector;
278df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Guimport android.support.v17.leanback.widget.OnItemViewSelectedListener;
2855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutimport android.support.v17.leanback.widget.Row;
293c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Guimport android.support.v17.leanback.widget.RowHeaderPresenter;
30bd80644186ff8622c565f067dc3a4136a4dc0586Dake Guimport android.support.v17.leanback.widget.SinglePresenterSelector;
313c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Guimport android.support.v17.leanback.widget.VerticalGridView;
320246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.support.v7.widget.RecyclerView;
334cf79b1c4d38a190317961891f9fd052836710fdCraig Stoutimport android.util.TypedValue;
3455c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutimport android.view.View;
3555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stoutimport android.view.ViewGroup;
362f97594742886d045ca1ce409ebc6e6e780452f6Dake Guimport android.view.View.OnLayoutChangeListener;
37e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stoutimport android.widget.FrameLayout;
3855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
3955c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout/**
4055c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * An internal fragment containing a list of row headers.
4155c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout */
42a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbournpublic class HeadersFragment extends BaseRowFragment {
433c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
44729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    /**
45729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     * Interface definition for a callback to be invoked when a header item is clicked.
46729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     */
47729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    public interface OnHeaderClickedListener {
48729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        /**
49729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * Called when a header item has been clicked.
50729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         *
51729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param viewHolder Row ViewHolder object corresponding to the selected Header.
52729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param row Row object corresponding to the selected Header.
53729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         */
54729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        void onHeaderClicked(RowHeaderPresenter.ViewHolder viewHolder, Row row);
553c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    }
563c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
57729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    /**
58729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     * Interface definition for a callback to be invoked when a header item is selected.
59729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     */
60729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    public interface OnHeaderViewSelectedListener {
61729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        /**
62729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * Called when a header item has been selected.
63729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         *
64729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param viewHolder Row ViewHolder object corresponding to the selected Header.
65729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param row Row object corresponding to the selected Header.
66729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         */
678df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu        void onHeaderSelected(RowHeaderPresenter.ViewHolder viewHolder, Row row);
688df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu    }
698df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu
708df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu    private OnHeaderViewSelectedListener mOnHeaderViewSelectedListener;
713c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    private OnHeaderClickedListener mOnHeaderClickedListener;
72d559710c8763acb37fd444735da7ee770b84df07Craig Stout    private boolean mHeadersEnabled = true;
73d559710c8763acb37fd444735da7ee770b84df07Craig Stout    private boolean mHeadersGone = false;
744cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    private int mBackgroundColor;
754cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    private boolean mBackgroundColorSet;
763c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
77bd80644186ff8622c565f067dc3a4136a4dc0586Dake Gu    private static final PresenterSelector sHeaderPresenter = new SinglePresenterSelector(
78268de3d2ea3de1be0725a80bbc79dd7b8b18617eCraig Stout            new RowHeaderPresenter(R.layout.lb_header));
7955c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
80a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn    public HeadersFragment() {
81bd80644186ff8622c565f067dc3a4136a4dc0586Dake Gu        setPresenterSelector(sHeaderPresenter);
8255c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
8355c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
843c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    public void setOnHeaderClickedListener(OnHeaderClickedListener listener) {
853c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        mOnHeaderClickedListener = listener;
8655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
8755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
888df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu    public void setOnHeaderViewSelectedListener(OnHeaderViewSelectedListener listener) {
898df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu        mOnHeaderViewSelectedListener = listener;
9055c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
9155c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
9255c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    @Override
9342752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    VerticalGridView findGridViewFromRoot(View view) {
947a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        return (VerticalGridView) view.findViewById(R.id.browse_headers);
957a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu    }
967a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu
977a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu    @Override
980246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    void onRowSelected(RecyclerView parent, RecyclerView.ViewHolder viewHolder,
990246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu            int position, int subposition) {
1008df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu        if (mOnHeaderViewSelectedListener != null) {
1010246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu            if (viewHolder != null && position >= 0) {
1020246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu                ItemBridgeAdapter.ViewHolder vh = (ItemBridgeAdapter.ViewHolder) viewHolder;
1038df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu                mOnHeaderViewSelectedListener.onHeaderSelected(
104729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                        (RowHeaderPresenter.ViewHolder) vh.getViewHolder(), (Row) vh.getItem());
105d736653bfee7fc83fc68d57d0e0bee5fa807287fDake Gu            } else {
1068df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu                mOnHeaderViewSelectedListener.onHeaderSelected(null, null);
1072f5fbc79d656fc4966119141aaef48161f08376fAndrew Wilson            }
10855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        }
10955c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
11055c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
1113c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    private final ItemBridgeAdapter.AdapterListener mAdapterListener =
1123c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            new ItemBridgeAdapter.AdapterListener() {
1133c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        @Override
114729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        public void onCreate(final ItemBridgeAdapter.ViewHolder viewHolder) {
1153c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            View headerView = viewHolder.getViewHolder().view;
1163c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            headerView.setOnClickListener(new View.OnClickListener() {
1173c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                @Override
1183c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                public void onClick(View v) {
1193c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                    if (mOnHeaderClickedListener != null) {
120729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                        mOnHeaderClickedListener.onHeaderClicked(
121729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                                (RowHeaderPresenter.ViewHolder) viewHolder.getViewHolder(),
122729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                                (Row) viewHolder.getItem());
1233c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                    }
1243c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                }
1253c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            });
1263c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            headerView.setFocusable(true);
1273c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            headerView.setFocusableInTouchMode(true);
128e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            if (mWrapper != null) {
129e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout                viewHolder.itemView.addOnLayoutChangeListener(sLayoutChangeListener);
130e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            } else {
131e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout                headerView.addOnLayoutChangeListener(sLayoutChangeListener);
132e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            }
1333c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        }
1343c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
1353c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    };
1363c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
1372f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    private static OnLayoutChangeListener sLayoutChangeListener = new OnLayoutChangeListener() {
1382f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        @Override
1392f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        public void onLayoutChange(View v, int left, int top, int right, int bottom,
1402f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu            int oldLeft, int oldTop, int oldRight, int oldBottom) {
1418e3566285de4ac771d6188f62fe947e23d371a3dKris Giesing            v.setPivotX(v.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL ? v.getWidth() : 0);
1422f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu            v.setPivotY(v.getMeasuredHeight() / 2);
1432f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        }
1442f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    };
1452f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu
1462f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    @Override
14742752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    int getLayoutResourceId() {
1482f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        return R.layout.lb_headers_fragment;
1492f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    }
1502f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu
1512f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    @Override
1522f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    public void onViewCreated(View view, Bundle savedInstanceState) {
1532f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        super.onViewCreated(view, savedInstanceState);
1544cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        final VerticalGridView listView = getVerticalGridView();
1554cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        if (listView == null) {
1564cf79b1c4d38a190317961891f9fd052836710fdCraig Stout            return;
1573c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        }
1584cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        if (getBridgeAdapter() != null) {
1594cf79b1c4d38a190317961891f9fd052836710fdCraig Stout            FocusHighlightHelper.setupHeaderItemFocusHighlight(listView);
1604cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        }
1617f43e18536eae40705d5c63830e9edb283f196adDake Gu        if (mBackgroundColorSet) {
1629a49e322d10c1a67860d812352a5ea710843b75eDake Gu            listView.setBackgroundColor(mBackgroundColor);
1637f43e18536eae40705d5c63830e9edb283f196adDake Gu            updateFadingEdgeToBrandColor(mBackgroundColor);
1649a49e322d10c1a67860d812352a5ea710843b75eDake Gu        } else {
1659a49e322d10c1a67860d812352a5ea710843b75eDake Gu            Drawable d = listView.getBackground();
1669a49e322d10c1a67860d812352a5ea710843b75eDake Gu            if (d instanceof ColorDrawable) {
1679a49e322d10c1a67860d812352a5ea710843b75eDake Gu                updateFadingEdgeToBrandColor(((ColorDrawable) d).getColor());
1689a49e322d10c1a67860d812352a5ea710843b75eDake Gu            }
1697f43e18536eae40705d5c63830e9edb283f196adDake Gu        }
170d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        updateListViewVisibility();
1713c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    }
1723c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
173d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu    private void updateListViewVisibility() {
1743c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        final VerticalGridView listView = getVerticalGridView();
1752f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        if (listView != null) {
1769652881bb6af5a2b76f89d683bd420336f4bf463Dake Gu            getView().setVisibility(mHeadersGone ? View.GONE : View.VISIBLE);
177d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu            if (!mHeadersGone) {
178d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                if (mHeadersEnabled) {
179d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                    listView.setChildrenVisibility(View.VISIBLE);
180d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                } else {
181d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                    listView.setChildrenVisibility(View.INVISIBLE);
182d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                }
183d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu            }
184d559710c8763acb37fd444735da7ee770b84df07Craig Stout        }
185d559710c8763acb37fd444735da7ee770b84df07Craig Stout    }
186d559710c8763acb37fd444735da7ee770b84df07Craig Stout
187d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu    void setHeadersEnabled(boolean enabled) {
188d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        mHeadersEnabled = enabled;
189d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        updateListViewVisibility();
190d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu    }
191d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu
192d559710c8763acb37fd444735da7ee770b84df07Craig Stout    void setHeadersGone(boolean gone) {
193d559710c8763acb37fd444735da7ee770b84df07Craig Stout        mHeadersGone = gone;
194d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        updateListViewVisibility();
1953c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    }
1963c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
1973659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu    static class NoOverlappingFrameLayout extends FrameLayout {
1983659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu
1993659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        public NoOverlappingFrameLayout(Context context) {
2003659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu            super(context);
2013659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        }
2023659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu
2033659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        /**
2043659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu         * Avoid creating hardware layer for header dock.
2053659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu         */
2063659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        @Override
2073659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        public boolean hasOverlappingRendering() {
2083659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu            return false;
2093659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        }
2103659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu    }
2113659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu
212e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout    // Wrapper needed because of conflict between RecyclerView's use of alpha
213e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout    // for ADD animations, and RowHeaderPresnter's use of alpha for selected level.
214e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout    private final ItemBridgeAdapter.Wrapper mWrapper = new ItemBridgeAdapter.Wrapper() {
215e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        @Override
216e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        public void wrap(View wrapper, View wrapped) {
217e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            ((FrameLayout) wrapper).addView(wrapped);
218e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        }
219e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout
220e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        @Override
221e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        public View createWrapper(View root) {
2223659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu            return new NoOverlappingFrameLayout(root.getContext());
223e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        }
224e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout    };
2253c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    @Override
22642752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    void updateAdapter() {
22755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        super.updateAdapter();
22855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        ItemBridgeAdapter adapter = getBridgeAdapter();
22955c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        if (adapter != null) {
2303c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            adapter.setAdapterListener(mAdapterListener);
231e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            adapter.setWrapper(mWrapper);
2323c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        }
2333c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        if (adapter != null && getVerticalGridView() != null) {
2343c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            FocusHighlightHelper.setupHeaderItemFocusHighlight(getVerticalGridView());
23555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        }
23655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
2374cf79b1c4d38a190317961891f9fd052836710fdCraig Stout
2384cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    void setBackgroundColor(int color) {
2394cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        mBackgroundColor = color;
2404cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        mBackgroundColorSet = true;
2414cf79b1c4d38a190317961891f9fd052836710fdCraig Stout
2429a49e322d10c1a67860d812352a5ea710843b75eDake Gu        if (getVerticalGridView() != null) {
2439a49e322d10c1a67860d812352a5ea710843b75eDake Gu            getVerticalGridView().setBackgroundColor(mBackgroundColor);
244b74149016854eb172c0ef121ccfef749c6ef7eb1Dake Gu            updateFadingEdgeToBrandColor(mBackgroundColor);
2457a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        }
2467a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu    }
2477a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu
248b74149016854eb172c0ef121ccfef749c6ef7eb1Dake Gu    private void updateFadingEdgeToBrandColor(int backgroundColor) {
2497a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        View fadingView = getView().findViewById(R.id.fade_out_edge);
2507a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        Drawable background = fadingView.getBackground();
2517a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        if (background instanceof GradientDrawable) {
2527a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu            background.mutate();
2537a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu            ((GradientDrawable) background).setColors(
254b74149016854eb172c0ef121ccfef749c6ef7eb1Dake Gu                    new int[] {Color.TRANSPARENT, backgroundColor});
2554cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        }
2564cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    }
2574cf79b1c4d38a190317961891f9fd052836710fdCraig Stout
25849e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    @Override
25949e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    void onTransitionStart() {
26049e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        super.onTransitionStart();
26149e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        if (!mHeadersEnabled) {
26249e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // When enabling headers fragment,  the RowHeaderView gets a focus but
26349e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // isShown() is still false because its parent is INVSIBILE, accessibility
26449e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // event is not sent.
26549e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // Workaround is: prevent focus to a child view during transition and put
26649e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // focus on it after transition is done.
26749e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            final VerticalGridView listView = getVerticalGridView();
26849e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            if (listView != null) {
26949e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
27049e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                if (listView.hasFocus()) {
27149e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                    listView.requestFocus();
27249e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                }
27349e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            }
27449e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        }
27549e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    }
27649e6a6cd81f3a1117b306323064c53230a7f01adDake Gu
27749e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    @Override
27849e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    void onTransitionEnd() {
27949e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        if (mHeadersEnabled) {
28049e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            final VerticalGridView listView = getVerticalGridView();
28149e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            if (listView != null) {
28249e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
28349e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                if (listView.hasFocus()) {
28449e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                    listView.requestFocus();
28549e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                }
28649e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            }
28749e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        }
28849e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        super.onTransitionEnd();
28949e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    }
29055c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout}
291