BaseRowSupportFragment.java revision 3f0f3eb255bde49549a77c0b5d252decaa2a0202
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    /**
1203f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu     * Sets the selected row position with smooth animation.
12161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu     */
12261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    public void setSelectedPosition(int position) {
1233f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu        setSelectedPosition(position, true);
1243f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu    }
1253f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu
1263f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu    /**
1273f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu     * Sets the selected row position.
1283f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu     */
1293f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu    public void setSelectedPosition(int position, boolean smooth) {
13061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mSelectedPosition = position;
13161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if(mVerticalGridView != null && mVerticalGridView.getAdapter() != null) {
1323f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu            if (smooth) {
1333f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu                mVerticalGridView.setSelectedPositionSmooth(position);
1343f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu            } else {
1353f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu                mVerticalGridView.setSelectedPosition(position);
1363f0f3eb255bde49549a77c0b5d252decaa2a0202Dake Gu            }
13761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
13861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
13961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
14061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    final VerticalGridView getVerticalGridView() {
14161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return mVerticalGridView;
14261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
14361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
14442752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    void updateAdapter() {
14561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mBridgeAdapter = null;
14661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
14761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mAdapter != null) {
14861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // If presenter selector is null, adapter ps will be used
14961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mBridgeAdapter = new ItemBridgeAdapter(mAdapter, mPresenterSelector);
15061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
15161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
15261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAdapter(mBridgeAdapter);
15361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            if (mBridgeAdapter != null && mSelectedPosition != -1) {
15461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                mVerticalGridView.setSelectedPosition(mSelectedPosition);
15561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            }
15661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
15761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
15861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
15942752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    Object getItem(Row row, int position) {
16061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (row instanceof ListRow) {
16161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            return ((ListRow) row).getAdapter().get(position);
16261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        } else {
16361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            return null;
16461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
16561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
16661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
16761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void onTransitionStart() {
16861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
16961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAnimateChildLayout(false);
17061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setPruneChild(false);
17161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setFocusSearchDisabled(true);
17261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
17361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
17461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
17561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void onTransitionEnd() {
17661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
17761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setAnimateChildLayout(true);
17861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setPruneChild(true);
17961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setFocusSearchDisabled(false);
18061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
18161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
18261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
18361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void setItemAlignment() {
18461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
18561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // align the top edge of item
18661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setItemAlignmentOffset(0);
18761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setItemAlignmentOffsetPercent(
18861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                    VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED);
18961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
19061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
19161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
19261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    void setWindowAlignmentFromTop(int alignedTop) {
19361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        if (mVerticalGridView != null) {
19461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            // align to a fixed position from top
19561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignmentOffset(alignedTop);
19661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignmentOffsetPercent(
19761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu                    VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED);
19861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu            mVerticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE);
19961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        }
20061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    }
20161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu}
202