BaseRowSupportFragment.java revision 42752c860a26deacca04ea9ebeb00ddb4d8ce2fc
16193c12a1897723c87b41f4e304a8cd04deef2dcDake Gu/* This file is auto-generated from BaseRowFragment.java.  DO NOT MODIFY. */
26193c12a1897723c87b41f4e304a8cd04deef2dcDake Gu
361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu/*
461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * Copyright (C) 2014 The Android Open Source Project
561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu *
661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * in compliance with the License. You may obtain a copy of the License at
861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu *
961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * http://www.apache.org/licenses/LICENSE-2.0
1061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu *
1161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * Unless required by applicable law or agreed to in writing, software distributed under the License
1261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * or implied. See the License for the specific language governing permissions and limitations under
1461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * the License.
1561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu */
1661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gupackage android.support.v17.leanback.app;
1761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
1861905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v4.app.Fragment;
1961905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.os.Bundle;
2061905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ObjectAdapter;
2161905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.PresenterSelector;
2261905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ItemBridgeAdapter;
2361905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.VerticalGridView;
2461905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.Row;
2561905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ListRow;
2661905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.OnChildSelectedListener;
2761905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.LayoutInflater;
2861905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.View;
2961905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.ViewGroup;
3061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
3161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu/**
3261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * An internal base class for a fragment containing a list of rows.
3361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu */
3461905b0b52c50018dcaebcd79699c39b8f28d622Dake Guabstract class BaseRowSupportFragment extends Fragment {
3561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private ObjectAdapter mAdapter;
3661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private VerticalGridView mVerticalGridView;
3761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private PresenterSelector mPresenterSelector;
3861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private ItemBridgeAdapter mBridgeAdapter;
3961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private int mSelectedPosition = -1;
4061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
4142752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    abstract int getLayoutResourceId();
4261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
4361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    private final OnChildSelectedListener mRowSelectedListener = new OnChildSelectedListener() {
4461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        @Override
4561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        public void onChildSelected(ViewGroup parent, View view, int position, long id) {
4661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            onRowSelected(parent, view, position, id);
4761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
4861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    };
4961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
5042752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    void onRowSelected(ViewGroup parent, View view, int position, long id) {
5161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
5261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
5361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    @Override
5461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public View onCreateView(LayoutInflater inflater, ViewGroup container,
5561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            Bundle savedInstanceState) {
5661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        View view = inflater.inflate(getLayoutResourceId(), container, false);
5761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mVerticalGridView = findGridViewFromRoot(view);
5861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return view;
5961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
6061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
6142752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    VerticalGridView findGridViewFromRoot(View view) {
6261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return (VerticalGridView) view;
6361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
6461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
6561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    @Override
6661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public void onViewCreated(View view, Bundle savedInstanceState) {
6761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mBridgeAdapter != null) {
6861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAdapter(mBridgeAdapter);
6961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            if (mSelectedPosition != -1) {
7061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                mVerticalGridView.setSelectedPosition(mSelectedPosition);
7161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            }
7261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
7361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mVerticalGridView.setOnChildSelectedListener(mRowSelectedListener);
7461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
7561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
7661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    @Override
7761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public void onDestroyView() {
7861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        super.onDestroyView();
7961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mVerticalGridView = null;
8061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
8161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
8261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
8361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Set the presenter selector used to create and bind views.
8461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
8561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final void setPresenterSelector(PresenterSelector presenterSelector) {
8661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mPresenterSelector = presenterSelector;
8761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        updateAdapter();
8861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
8961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
9061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
9161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Get the presenter selector used to create and bind views.
9261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
9361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final PresenterSelector getPresenterSelector() {
9461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mPresenterSelector;
9561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
9661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
9761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
9861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Sets the adapter for the fragment.
9961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
10061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final void setAdapter(ObjectAdapter rowsAdapter) {
10161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mAdapter = rowsAdapter;
10261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        updateAdapter();
10361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
10461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
10561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
10661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Returns the list of rows.
10761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
10861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public final ObjectAdapter getAdapter() {
10961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mAdapter;
11061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
11161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
11261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
11361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Returns the bridge adapter.
11461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
11542752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    final ItemBridgeAdapter getBridgeAdapter() {
11661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mBridgeAdapter;
11761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
11861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
11961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    /**
12061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     * Set the selected item position.
12161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
12261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public void setSelectedPosition(int position) {
12361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mSelectedPosition = position;
12461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if(mVerticalGridView != null && mVerticalGridView.getAdapter() != null) {
12561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setSelectedPositionSmooth(position);
12661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
12761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
12861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
12961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    final VerticalGridView getVerticalGridView() {
13061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mVerticalGridView;
13161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
13261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
13342752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    void updateAdapter() {
13461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mBridgeAdapter = null;
13561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
13661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mAdapter != null) {
13761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // If presenter selector is null, adapter ps will be used
13861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mBridgeAdapter = new ItemBridgeAdapter(mAdapter, mPresenterSelector);
13961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
14061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
14161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAdapter(mBridgeAdapter);
14261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            if (mBridgeAdapter != null && mSelectedPosition != -1) {
14361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                mVerticalGridView.setSelectedPosition(mSelectedPosition);
14461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            }
14561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
14661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
14761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
14842752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    Object getItem(Row row, int position) {
14961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (row instanceof ListRow) {
15061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            return ((ListRow) row).getAdapter().get(position);
15161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        } else {
15261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            return null;
15361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
15461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
15561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
15661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void onTransitionStart() {
15761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
15861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAnimateChildLayout(false);
15961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setPruneChild(false);
16061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setFocusSearchDisabled(true);
16161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
16261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
16361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
16461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void onTransitionEnd() {
16561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
16661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAnimateChildLayout(true);
16761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setPruneChild(true);
16861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setFocusSearchDisabled(false);
16961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
17061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
17161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
17261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void setItemAlignment() {
17361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
17461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // align the top edge of item
17561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setItemAlignmentOffset(0);
17661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setItemAlignmentOffsetPercent(
17761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                    VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED);
17861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
17961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
18061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
18161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void setWindowAlignmentFromTop(int alignedTop) {
18261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
18361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // align to a fixed position from top
18461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignmentOffset(alignedTop);
18561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignmentOffsetPercent(
18661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                    VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED);
18761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE);
18861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
18961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
19061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu}
191