1 // CHECKSTYLE:OFF Generated code 2 /* This file is auto-generated from BaseRowSupportFragment.java. DO NOT MODIFY. */ 3 4 /* 5 * Copyright (C) 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 8 * in compliance with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed under the License 13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 14 * or implied. See the License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 package android.support.v17.leanback.app; 18 19 import android.os.Bundle; 20 import android.support.v17.leanback.widget.ItemBridgeAdapter; 21 import android.support.v17.leanback.widget.ListRow; 22 import android.support.v17.leanback.widget.ObjectAdapter; 23 import android.support.v17.leanback.widget.OnChildViewHolderSelectedListener; 24 import android.support.v17.leanback.widget.PresenterSelector; 25 import android.support.v17.leanback.widget.Row; 26 import android.support.v17.leanback.widget.VerticalGridView; 27 import android.app.Fragment; 28 import android.support.v7.widget.RecyclerView; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.ViewGroup; 32 33 /** 34 * An internal base class for a fragment containing a list of rows. 35 */ 36 abstract class BaseRowFragment extends Fragment { 37 private static final String CURRENT_SELECTED_POSITION = "currentSelectedPosition"; 38 private ObjectAdapter mAdapter; 39 VerticalGridView mVerticalGridView; 40 private PresenterSelector mPresenterSelector; 41 final ItemBridgeAdapter mBridgeAdapter = new ItemBridgeAdapter(); 42 int mSelectedPosition = -1; 43 private boolean mPendingTransitionPrepare; 44 private LateSelectionObserver mLateSelectionObserver = new LateSelectionObserver(); 45 getLayoutResourceId()46 abstract int getLayoutResourceId(); 47 48 private final OnChildViewHolderSelectedListener mRowSelectedListener = 49 new OnChildViewHolderSelectedListener() { 50 @Override 51 public void onChildViewHolderSelected(RecyclerView parent, 52 RecyclerView.ViewHolder view, int position, int subposition) { 53 if (!mLateSelectionObserver.mIsLateSelection) { 54 mSelectedPosition = position; 55 onRowSelected(parent, view, position, subposition); 56 } 57 } 58 }; 59 onRowSelected(RecyclerView parent, RecyclerView.ViewHolder view, int position, int subposition)60 void onRowSelected(RecyclerView parent, RecyclerView.ViewHolder view, 61 int position, int subposition) { 62 } 63 64 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)65 public View onCreateView(LayoutInflater inflater, ViewGroup container, 66 Bundle savedInstanceState) { 67 View view = inflater.inflate(getLayoutResourceId(), container, false); 68 mVerticalGridView = findGridViewFromRoot(view); 69 if (mPendingTransitionPrepare) { 70 mPendingTransitionPrepare = false; 71 onTransitionPrepare(); 72 } 73 return view; 74 } 75 findGridViewFromRoot(View view)76 VerticalGridView findGridViewFromRoot(View view) { 77 return (VerticalGridView) view; 78 } 79 80 @Override onViewCreated(View view, Bundle savedInstanceState)81 public void onViewCreated(View view, Bundle savedInstanceState) { 82 if (savedInstanceState != null) { 83 mSelectedPosition = savedInstanceState.getInt(CURRENT_SELECTED_POSITION, -1); 84 } 85 setAdapterAndSelection(); 86 mVerticalGridView.setOnChildViewHolderSelectedListener(mRowSelectedListener); 87 } 88 89 /** 90 * This class waits for the adapter to be updated before setting the selected 91 * row. 92 */ 93 private class LateSelectionObserver extends RecyclerView.AdapterDataObserver { 94 boolean mIsLateSelection = false; 95 LateSelectionObserver()96 LateSelectionObserver() { 97 } 98 99 @Override onChanged()100 public void onChanged() { 101 performLateSelection(); 102 } 103 104 @Override onItemRangeInserted(int positionStart, int itemCount)105 public void onItemRangeInserted(int positionStart, int itemCount) { 106 performLateSelection(); 107 } 108 startLateSelection()109 void startLateSelection() { 110 mIsLateSelection = true; 111 mBridgeAdapter.registerAdapterDataObserver(this); 112 } 113 performLateSelection()114 void performLateSelection() { 115 clear(); 116 if (mVerticalGridView != null) { 117 mVerticalGridView.setSelectedPosition(mSelectedPosition); 118 } 119 } 120 clear()121 void clear() { 122 if (mIsLateSelection) { 123 mIsLateSelection = false; 124 mBridgeAdapter.unregisterAdapterDataObserver(this); 125 } 126 } 127 } 128 setAdapterAndSelection()129 void setAdapterAndSelection() { 130 if (mAdapter == null) { 131 // delay until ItemBridgeAdapter has wrappedAdapter. Once we assign ItemBridgeAdapter 132 // to RecyclerView, it will not be allowed to change "hasStableId" to true. 133 return; 134 } 135 if (mVerticalGridView.getAdapter() != mBridgeAdapter) { 136 // avoid extra layout if ItemBridgeAdapter was already set. 137 mVerticalGridView.setAdapter(mBridgeAdapter); 138 } 139 // We don't set the selected position unless we've data in the adapter. 140 boolean lateSelection = mBridgeAdapter.getItemCount() == 0 && mSelectedPosition >= 0; 141 if (lateSelection) { 142 mLateSelectionObserver.startLateSelection(); 143 } else if (mSelectedPosition >= 0) { 144 mVerticalGridView.setSelectedPosition(mSelectedPosition); 145 } 146 } 147 148 @Override onDestroyView()149 public void onDestroyView() { 150 super.onDestroyView(); 151 mLateSelectionObserver.clear(); 152 mVerticalGridView = null; 153 } 154 155 @Override onSaveInstanceState(Bundle outState)156 public void onSaveInstanceState(Bundle outState) { 157 super.onSaveInstanceState(outState); 158 outState.putInt(CURRENT_SELECTED_POSITION, mSelectedPosition); 159 } 160 161 /** 162 * Set the presenter selector used to create and bind views. 163 */ setPresenterSelector(PresenterSelector presenterSelector)164 public final void setPresenterSelector(PresenterSelector presenterSelector) { 165 mPresenterSelector = presenterSelector; 166 updateAdapter(); 167 } 168 169 /** 170 * Get the presenter selector used to create and bind views. 171 */ getPresenterSelector()172 public final PresenterSelector getPresenterSelector() { 173 return mPresenterSelector; 174 } 175 176 /** 177 * Sets the adapter that represents a list of rows. 178 * @param rowsAdapter Adapter that represents list of rows. 179 */ setAdapter(ObjectAdapter rowsAdapter)180 public final void setAdapter(ObjectAdapter rowsAdapter) { 181 mAdapter = rowsAdapter; 182 updateAdapter(); 183 } 184 185 /** 186 * Returns the Adapter that represents list of rows. 187 * @return Adapter that represents list of rows. 188 */ getAdapter()189 public final ObjectAdapter getAdapter() { 190 return mAdapter; 191 } 192 193 /** 194 * Returns the RecyclerView.Adapter that wraps {@link #getAdapter()}. 195 * @return The RecyclerView.Adapter that wraps {@link #getAdapter()}. 196 */ getBridgeAdapter()197 public final ItemBridgeAdapter getBridgeAdapter() { 198 return mBridgeAdapter; 199 } 200 201 /** 202 * Sets the selected row position with smooth animation. 203 */ setSelectedPosition(int position)204 public void setSelectedPosition(int position) { 205 setSelectedPosition(position, true); 206 } 207 208 /** 209 * Gets position of currently selected row. 210 * @return Position of currently selected row. 211 */ getSelectedPosition()212 public int getSelectedPosition() { 213 return mSelectedPosition; 214 } 215 216 /** 217 * Sets the selected row position. 218 */ setSelectedPosition(int position, boolean smooth)219 public void setSelectedPosition(int position, boolean smooth) { 220 if (mSelectedPosition == position) { 221 return; 222 } 223 mSelectedPosition = position; 224 if (mVerticalGridView != null) { 225 if (mLateSelectionObserver.mIsLateSelection) { 226 return; 227 } 228 if (smooth) { 229 mVerticalGridView.setSelectedPositionSmooth(position); 230 } else { 231 mVerticalGridView.setSelectedPosition(position); 232 } 233 } 234 } 235 getVerticalGridView()236 public final VerticalGridView getVerticalGridView() { 237 return mVerticalGridView; 238 } 239 updateAdapter()240 void updateAdapter() { 241 mBridgeAdapter.setAdapter(mAdapter); 242 mBridgeAdapter.setPresenter(mPresenterSelector); 243 244 if (mVerticalGridView != null) { 245 setAdapterAndSelection(); 246 } 247 } 248 getItem(Row row, int position)249 Object getItem(Row row, int position) { 250 if (row instanceof ListRow) { 251 return ((ListRow) row).getAdapter().get(position); 252 } else { 253 return null; 254 } 255 } 256 onTransitionPrepare()257 public boolean onTransitionPrepare() { 258 if (mVerticalGridView != null) { 259 mVerticalGridView.setAnimateChildLayout(false); 260 mVerticalGridView.setScrollEnabled(false); 261 return true; 262 } 263 mPendingTransitionPrepare = true; 264 return false; 265 } 266 onTransitionStart()267 public void onTransitionStart() { 268 if (mVerticalGridView != null) { 269 mVerticalGridView.setPruneChild(false); 270 mVerticalGridView.setLayoutFrozen(true); 271 mVerticalGridView.setFocusSearchDisabled(true); 272 } 273 } 274 onTransitionEnd()275 public void onTransitionEnd() { 276 // be careful that fragment might be destroyed before header transition ends. 277 if (mVerticalGridView != null) { 278 mVerticalGridView.setLayoutFrozen(false); 279 mVerticalGridView.setAnimateChildLayout(true); 280 mVerticalGridView.setPruneChild(true); 281 mVerticalGridView.setFocusSearchDisabled(false); 282 mVerticalGridView.setScrollEnabled(true); 283 } 284 } 285 setAlignment(int windowAlignOffsetTop)286 public void setAlignment(int windowAlignOffsetTop) { 287 if (mVerticalGridView != null) { 288 // align the top edge of item 289 mVerticalGridView.setItemAlignmentOffset(0); 290 mVerticalGridView.setItemAlignmentOffsetPercent( 291 VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED); 292 293 // align to a fixed position from top 294 mVerticalGridView.setWindowAlignmentOffset(windowAlignOffsetTop); 295 mVerticalGridView.setWindowAlignmentOffsetPercent( 296 VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED); 297 mVerticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE); 298 } 299 } 300 } 301