161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu/*
261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * Copyright (C) 2014 The Android Open Source Project
361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu *
461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * in compliance with the License. You may obtain a copy of the License at
661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu *
761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * http://www.apache.org/licenses/LICENSE-2.0
861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu *
961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * Unless required by applicable law or agreed to in writing, software distributed under the License
1061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * or implied. See the License for the specific language governing permissions and limitations under
1261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * the License.
1361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu */
1461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gupackage android.support.v17.leanback.app;
1561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
1661905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.os.Bundle;
1761905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ItemBridgeAdapter;
1861905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ListRow;
192f5ebf3f6f7bb6a24856f389e369b247118ba119susnataimport android.support.v17.leanback.widget.ObjectAdapter;
200246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.support.v17.leanback.widget.OnChildViewHolderSelectedListener;
212f5ebf3f6f7bb6a24856f389e369b247118ba119susnataimport android.support.v17.leanback.widget.PresenterSelector;
222f5ebf3f6f7bb6a24856f389e369b247118ba119susnataimport android.support.v17.leanback.widget.Row;
232f5ebf3f6f7bb6a24856f389e369b247118ba119susnataimport android.support.v17.leanback.widget.VerticalGridView;
24b86a6c2c71514e0cb693597a955e224811b90f96Dake Guimport android.support.v4.app.Fragment;
250246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.support.v7.widget.RecyclerView;
2661905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.LayoutInflater;
2761905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.View;
2861905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.ViewGroup;
2961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
3061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu/**
3161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * An internal base class for a fragment containing a list of rows.
3261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu */
3361905b0b52c50018dcaebcd79699c39b8f28d622Dake Guabstract class BaseRowSupportFragment extends Fragment {
34fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak    private static final String CURRENT_SELECTED_POSITION = "currentSelectedPosition";
3561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private ObjectAdapter mAdapter;
3699ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas    VerticalGridView mVerticalGridView;
3761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private PresenterSelector mPresenterSelector;
3850cf9ada93e50e906f20f5edf595234ada196d45Dake Gu    final ItemBridgeAdapter mBridgeAdapter = new ItemBridgeAdapter();
3999ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas    int mSelectedPosition = -1;
40902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu    private boolean mPendingTransitionPrepare;
41bb0a680c10b84b83833a59634373140f8bd0750csusnata    private LateSelectionObserver mLateSelectionObserver = new LateSelectionObserver();
4261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
4342752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    abstract int getLayoutResourceId();
4461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
450246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    private final OnChildViewHolderSelectedListener mRowSelectedListener =
460246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu            new OnChildViewHolderSelectedListener() {
470246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu                @Override
480246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu                public void onChildViewHolderSelected(RecyclerView parent,
490246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu                        RecyclerView.ViewHolder view, int position, int subposition) {
50514b6d101c6b7d4b7151cf8789424bde66fab511Dake Gu                    if (!mLateSelectionObserver.mIsLateSelection) {
51514b6d101c6b7d4b7151cf8789424bde66fab511Dake Gu                        mSelectedPosition = position;
52514b6d101c6b7d4b7151cf8789424bde66fab511Dake Gu                        onRowSelected(parent, view, position, subposition);
53514b6d101c6b7d4b7151cf8789424bde66fab511Dake Gu                    }
540246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu                }
550246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu            };
5661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
570246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    void onRowSelected(RecyclerView parent, RecyclerView.ViewHolder view,
580246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu            int position, int subposition) {
5961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
6061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
6161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    @Override
6261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public View onCreateView(LayoutInflater inflater, ViewGroup container,
6361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            Bundle savedInstanceState) {
6461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        View view = inflater.inflate(getLayoutResourceId(), container, false);
6561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mVerticalGridView = findGridViewFromRoot(view);
66902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        if (mPendingTransitionPrepare) {
67902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu            mPendingTransitionPrepare = false;
68902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu            onTransitionPrepare();
69902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        }
7061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return view;
7161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
7261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
7342752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    VerticalGridView findGridViewFromRoot(View view) {
7461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return (VerticalGridView) view;
7561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
7661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
7761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    @Override
7861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public void onViewCreated(View view, Bundle savedInstanceState) {
79fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak        if (savedInstanceState != null) {
80fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak            mSelectedPosition = savedInstanceState.getInt(CURRENT_SELECTED_POSITION, -1);
81fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak        }
8250cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        setAdapterAndSelection();
83bb0a680c10b84b83833a59634373140f8bd0750csusnata        mVerticalGridView.setOnChildViewHolderSelectedListener(mRowSelectedListener);
84bb0a680c10b84b83833a59634373140f8bd0750csusnata    }
85bb0a680c10b84b83833a59634373140f8bd0750csusnata
86bb0a680c10b84b83833a59634373140f8bd0750csusnata    /**
87bb0a680c10b84b83833a59634373140f8bd0750csusnata     * This class waits for the adapter to be updated before setting the selected
88bb0a680c10b84b83833a59634373140f8bd0750csusnata     * row.
89bb0a680c10b84b83833a59634373140f8bd0750csusnata     */
90bb0a680c10b84b83833a59634373140f8bd0750csusnata    private class LateSelectionObserver extends RecyclerView.AdapterDataObserver {
91bb0a680c10b84b83833a59634373140f8bd0750csusnata        boolean mIsLateSelection = false;
92bb0a680c10b84b83833a59634373140f8bd0750csusnata
9399ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        LateSelectionObserver() {
9499ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        }
9599ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas
9615375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas        @Override
97bb0a680c10b84b83833a59634373140f8bd0750csusnata        public void onChanged() {
98bb0a680c10b84b83833a59634373140f8bd0750csusnata            performLateSelection();
99bb0a680c10b84b83833a59634373140f8bd0750csusnata        }
100bb0a680c10b84b83833a59634373140f8bd0750csusnata
10115375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas        @Override
102bb0a680c10b84b83833a59634373140f8bd0750csusnata        public void onItemRangeInserted(int positionStart, int itemCount) {
103bb0a680c10b84b83833a59634373140f8bd0750csusnata            performLateSelection();
104bb0a680c10b84b83833a59634373140f8bd0750csusnata        }
105bb0a680c10b84b83833a59634373140f8bd0750csusnata
106bb0a680c10b84b83833a59634373140f8bd0750csusnata        void startLateSelection() {
107bb0a680c10b84b83833a59634373140f8bd0750csusnata            mIsLateSelection = true;
108bb0a680c10b84b83833a59634373140f8bd0750csusnata            mBridgeAdapter.registerAdapterDataObserver(this);
109bb0a680c10b84b83833a59634373140f8bd0750csusnata        }
110bb0a680c10b84b83833a59634373140f8bd0750csusnata
111bb0a680c10b84b83833a59634373140f8bd0750csusnata        void performLateSelection() {
112bb0a680c10b84b83833a59634373140f8bd0750csusnata            clear();
113bb0a680c10b84b83833a59634373140f8bd0750csusnata            if (mVerticalGridView != null) {
11461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                mVerticalGridView.setSelectedPosition(mSelectedPosition);
11561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            }
11661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
117bb0a680c10b84b83833a59634373140f8bd0750csusnata
118bb0a680c10b84b83833a59634373140f8bd0750csusnata        void clear() {
119bb0a680c10b84b83833a59634373140f8bd0750csusnata            if (mIsLateSelection) {
120bb0a680c10b84b83833a59634373140f8bd0750csusnata                mIsLateSelection = false;
121bb0a680c10b84b83833a59634373140f8bd0750csusnata                mBridgeAdapter.unregisterAdapterDataObserver(this);
122bb0a680c10b84b83833a59634373140f8bd0750csusnata            }
123bb0a680c10b84b83833a59634373140f8bd0750csusnata        }
124bb0a680c10b84b83833a59634373140f8bd0750csusnata    }
125bb0a680c10b84b83833a59634373140f8bd0750csusnata
126bb0a680c10b84b83833a59634373140f8bd0750csusnata    void setAdapterAndSelection() {
127c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu        if (mAdapter == null) {
128c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu            // delay until ItemBridgeAdapter has wrappedAdapter. Once we assign ItemBridgeAdapter
129c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu            // to RecyclerView, it will not be allowed to change "hasStableId" to true.
130c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu            return;
131c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu        }
132c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu        if (mVerticalGridView.getAdapter() != mBridgeAdapter) {
133c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu            // avoid extra layout if ItemBridgeAdapter was already set.
134c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu            mVerticalGridView.setAdapter(mBridgeAdapter);
135c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu        }
136bb0a680c10b84b83833a59634373140f8bd0750csusnata        // We don't set the selected position unless we've data in the adapter.
137bb0a680c10b84b83833a59634373140f8bd0750csusnata        boolean lateSelection = mBridgeAdapter.getItemCount() == 0 && mSelectedPosition >= 0;
138bb0a680c10b84b83833a59634373140f8bd0750csusnata        if (lateSelection) {
139bb0a680c10b84b83833a59634373140f8bd0750csusnata            mLateSelectionObserver.startLateSelection();
140bb0a680c10b84b83833a59634373140f8bd0750csusnata        } else if (mSelectedPosition >= 0) {
141bb0a680c10b84b83833a59634373140f8bd0750csusnata            mVerticalGridView.setSelectedPosition(mSelectedPosition);
142bb0a680c10b84b83833a59634373140f8bd0750csusnata        }
14361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
14461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
14561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    @Override
14661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public void onDestroyView() {
14761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        super.onDestroyView();
148bb0a680c10b84b83833a59634373140f8bd0750csusnata        mLateSelectionObserver.clear();
14961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mVerticalGridView = null;
15061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
15161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
152fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak    @Override
153fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak    public void onSaveInstanceState(Bundle outState) {
154fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak        super.onSaveInstanceState(outState);
155fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak        outState.putInt(CURRENT_SELECTED_POSITION, mSelectedPosition);
156fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak    }
157fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak
15861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
15961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Set the presenter selector used to create and bind views.
16061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
16161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final void setPresenterSelector(PresenterSelector presenterSelector) {
16261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mPresenterSelector = presenterSelector;
16361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        updateAdapter();
16461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
16561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
16661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
16761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Get the presenter selector used to create and bind views.
16861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
16961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final PresenterSelector getPresenterSelector() {
17061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mPresenterSelector;
17161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
17261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
17361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
174c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu     * Sets the adapter that represents a list of rows.
175c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu     * @param rowsAdapter Adapter that represents list of rows.
17661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
17761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final void setAdapter(ObjectAdapter rowsAdapter) {
17861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mAdapter = rowsAdapter;
17961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        updateAdapter();
18061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
18161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
18261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
183c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu     * Returns the Adapter that represents list of rows.
184c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu     * @return Adapter that represents list of rows.
18561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
18661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final ObjectAdapter getAdapter() {
18761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mAdapter;
18861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
18961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
19061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
191c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu     * Returns the RecyclerView.Adapter that wraps {@link #getAdapter()}.
192c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu     * @return The RecyclerView.Adapter that wraps {@link #getAdapter()}.
19361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
194c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu    public final ItemBridgeAdapter getBridgeAdapter() {
19561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mBridgeAdapter;
19661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
19761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
19861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
1993f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu     * Sets the selected row position with smooth animation.
20061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
20161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public void setSelectedPosition(int position) {
2023f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu        setSelectedPosition(position, true);
2033f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu    }
2043f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu
2053f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu    /**
2060d73d42df7cf4b325b8f49660e3326204915ce8fDake Gu     * Gets position of currently selected row.
2070d73d42df7cf4b325b8f49660e3326204915ce8fDake Gu     * @return Position of currently selected row.
2080d73d42df7cf4b325b8f49660e3326204915ce8fDake Gu     */
2092f1bd591e01fe93f605844d4a10cde7116315b7cDake Gu    public int getSelectedPosition() {
2100d73d42df7cf4b325b8f49660e3326204915ce8fDake Gu        return mSelectedPosition;
2110d73d42df7cf4b325b8f49660e3326204915ce8fDake Gu    }
2120d73d42df7cf4b325b8f49660e3326204915ce8fDake Gu
2130d73d42df7cf4b325b8f49660e3326204915ce8fDake Gu    /**
2143f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu     * Sets the selected row position.
2153f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu     */
2163f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu    public void setSelectedPosition(int position, boolean smooth) {
2170d841b3454f896da58deb506ca22730bfd04f34fDake Gu        if (mSelectedPosition == position) {
2180d841b3454f896da58deb506ca22730bfd04f34fDake Gu            return;
2190d841b3454f896da58deb506ca22730bfd04f34fDake Gu        }
22061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mSelectedPosition = position;
22150cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        if (mVerticalGridView != null) {
222bb0a680c10b84b83833a59634373140f8bd0750csusnata            if (mLateSelectionObserver.mIsLateSelection) {
223bb0a680c10b84b83833a59634373140f8bd0750csusnata                return;
224bb0a680c10b84b83833a59634373140f8bd0750csusnata            }
2253f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu            if (smooth) {
2263f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu                mVerticalGridView.setSelectedPositionSmooth(position);
2273f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu            } else {
2283f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu                mVerticalGridView.setSelectedPosition(position);
2293f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu            }
23061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
23161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
23261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
233935b033edfd9d03129fce6a5b7a1d3a327b74f91susnata    public final VerticalGridView getVerticalGridView() {
23461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mVerticalGridView;
23561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
23661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
23742752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    void updateAdapter() {
23850cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        mBridgeAdapter.setAdapter(mAdapter);
23950cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        mBridgeAdapter.setPresenter(mPresenterSelector);
24061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
24161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
242bb0a680c10b84b83833a59634373140f8bd0750csusnata            setAdapterAndSelection();
24361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
24461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
24561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
24642752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    Object getItem(Row row, int position) {
24761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (row instanceof ListRow) {
24861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            return ((ListRow) row).getAdapter().get(position);
24961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        } else {
25061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            return null;
25161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
25261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
25361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
2542f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    public boolean onTransitionPrepare() {
25561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
25661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAnimateChildLayout(false);
257902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu            mVerticalGridView.setScrollEnabled(false);
258902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu            return true;
25961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
260902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        mPendingTransitionPrepare = true;
261902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        return false;
262902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu    }
263902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu
2642f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    public void onTransitionStart() {
2654a4e0872bdf0548188e81740eb62085215e7f976Dake Gu        if (mVerticalGridView != null) {
2664a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setPruneChild(false);
2674a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setLayoutFrozen(true);
2684a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setFocusSearchDisabled(true);
2694a4e0872bdf0548188e81740eb62085215e7f976Dake Gu        }
27061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
27161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
2722f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    public void onTransitionEnd() {
2734a4e0872bdf0548188e81740eb62085215e7f976Dake Gu        // be careful that fragment might be destroyed before header transition ends.
2744a4e0872bdf0548188e81740eb62085215e7f976Dake Gu        if (mVerticalGridView != null) {
2754a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setLayoutFrozen(false);
2764a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setAnimateChildLayout(true);
2774a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setPruneChild(true);
2784a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setFocusSearchDisabled(false);
2794a4e0872bdf0548188e81740eb62085215e7f976Dake Gu            mVerticalGridView.setScrollEnabled(true);
2804a4e0872bdf0548188e81740eb62085215e7f976Dake Gu        }
28161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
28261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
2832f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    public void setAlignment(int windowAlignOffsetTop) {
28461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
28561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // align the top edge of item
28661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setItemAlignmentOffset(0);
28761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setItemAlignmentOffsetPercent(
28861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                    VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED);
28961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
29061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // align to a fixed position from top
2912f5ebf3f6f7bb6a24856f389e369b247118ba119susnata            mVerticalGridView.setWindowAlignmentOffset(windowAlignOffsetTop);
29261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignmentOffsetPercent(
29361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                    VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED);
29461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE);
29561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
29661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
29761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu}
298