BaseRowSupportFragment.java revision 61905b0b52c50018dcaebcd79699c39b8f28d622
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.support.v4.app.Fragment; 1761905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.os.Bundle; 1861905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ObjectAdapter; 1961905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.PresenterSelector; 2061905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ItemBridgeAdapter; 2161905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.VerticalGridView; 2261905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.Row; 2361905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.ListRow; 2461905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.support.v17.leanback.widget.OnChildSelectedListener; 2561905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.LayoutInflater; 2661905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.View; 2761905b0b52c50018dcaebcd79699c39b8f28d622Dake Guimport android.view.ViewGroup; 2861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu 2961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu/** 3061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu * An internal base class for a fragment containing a list of rows. 3161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu */ 3261905b0b52c50018dcaebcd79699c39b8f28d622Dake Guabstract class BaseRowSupportFragment extends Fragment { 3361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu private ObjectAdapter mAdapter; 3461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu private VerticalGridView mVerticalGridView; 3561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu private PresenterSelector mPresenterSelector; 3661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu private ItemBridgeAdapter mBridgeAdapter; 3761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu private int mSelectedPosition = -1; 3861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu protected int mReparentHeaderId; 3961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu protected boolean mInTransition; 4061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu 4161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu abstract protected 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 5061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu protected 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 6161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu protected 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 */ 11561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu protected 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 13361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu protected 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 14861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu protected 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 setReparentHeaderId(int reparentId) { 15761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mReparentHeaderId = reparentId; 15861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 15961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu 16061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu void onTransitionStart() { 16161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mInTransition = true; 16261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu if (mVerticalGridView != null) { 16361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setAnimateChildLayout(false); 16461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setPruneChild(false); 16561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setFocusSearchDisabled(true); 16661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 16761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 16861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu 16961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu void onTransitionEnd() { 17061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu if (mVerticalGridView != null) { 17161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setAnimateChildLayout(true); 17261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setPruneChild(true); 17361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setFocusSearchDisabled(false); 17461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 17561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mInTransition = false; 17661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 17761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu 17861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu void setItemAlignment() { 17961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu if (mVerticalGridView != null) { 18061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu // align the top edge of item 18161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setItemAlignmentOffset(0); 18261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setItemAlignmentOffsetPercent( 18361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED); 18461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 18561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 18661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu 18761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu void setWindowAlignmentFromTop(int alignedTop) { 18861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu if (mVerticalGridView != null) { 18961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu // align to a fixed position from top 19061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setWindowAlignmentOffset(alignedTop); 19161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setWindowAlignmentOffsetPercent( 19261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED); 19361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu mVerticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE); 19461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 19561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu } 19661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu} 197