1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package android.support.v17.leanback.app; 15 16 import android.app.Fragment; 17 import android.os.Bundle; 18 import android.support.v17.leanback.widget.ObjectAdapter; 19 import android.support.v17.leanback.widget.PresenterSelector; 20 import android.support.v17.leanback.widget.ItemBridgeAdapter; 21 import android.support.v17.leanback.widget.VerticalGridView; 22 import android.support.v17.leanback.widget.Row; 23 import android.support.v17.leanback.widget.ListRow; 24 import android.support.v17.leanback.widget.OnChildSelectedListener; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.ViewGroup; 28 29 /** 30 * An internal base class for a fragment containing a list of rows. 31 */ 32 abstract class BaseRowFragment extends Fragment { 33 private ObjectAdapter mAdapter; 34 private VerticalGridView mVerticalGridView; 35 private PresenterSelector mPresenterSelector; 36 private ItemBridgeAdapter mBridgeAdapter; 37 private int mSelectedPosition = -1; 38 getLayoutResourceId()39 abstract int getLayoutResourceId(); 40 41 private final OnChildSelectedListener mRowSelectedListener = new OnChildSelectedListener() { 42 @Override 43 public void onChildSelected(ViewGroup parent, View view, int position, long id) { 44 onRowSelected(parent, view, position, id); 45 } 46 }; 47 onRowSelected(ViewGroup parent, View view, int position, long id)48 void onRowSelected(ViewGroup parent, View view, int position, long id) { 49 } 50 51 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)52 public View onCreateView(LayoutInflater inflater, ViewGroup container, 53 Bundle savedInstanceState) { 54 View view = inflater.inflate(getLayoutResourceId(), container, false); 55 mVerticalGridView = findGridViewFromRoot(view); 56 return view; 57 } 58 findGridViewFromRoot(View view)59 VerticalGridView findGridViewFromRoot(View view) { 60 return (VerticalGridView) view; 61 } 62 63 @Override onViewCreated(View view, Bundle savedInstanceState)64 public void onViewCreated(View view, Bundle savedInstanceState) { 65 if (mBridgeAdapter != null) { 66 mVerticalGridView.setAdapter(mBridgeAdapter); 67 if (mSelectedPosition != -1) { 68 mVerticalGridView.setSelectedPosition(mSelectedPosition); 69 } 70 } 71 mVerticalGridView.setOnChildSelectedListener(mRowSelectedListener); 72 } 73 74 @Override onDestroyView()75 public void onDestroyView() { 76 super.onDestroyView(); 77 mVerticalGridView = null; 78 } 79 80 /** 81 * Set the presenter selector used to create and bind views. 82 */ setPresenterSelector(PresenterSelector presenterSelector)83 public final void setPresenterSelector(PresenterSelector presenterSelector) { 84 mPresenterSelector = presenterSelector; 85 updateAdapter(); 86 } 87 88 /** 89 * Get the presenter selector used to create and bind views. 90 */ getPresenterSelector()91 public final PresenterSelector getPresenterSelector() { 92 return mPresenterSelector; 93 } 94 95 /** 96 * Sets the adapter for the fragment. 97 */ setAdapter(ObjectAdapter rowsAdapter)98 public final void setAdapter(ObjectAdapter rowsAdapter) { 99 mAdapter = rowsAdapter; 100 updateAdapter(); 101 } 102 103 /** 104 * Returns the list of rows. 105 */ getAdapter()106 public final ObjectAdapter getAdapter() { 107 return mAdapter; 108 } 109 110 /** 111 * Returns the bridge adapter. 112 */ getBridgeAdapter()113 final ItemBridgeAdapter getBridgeAdapter() { 114 return mBridgeAdapter; 115 } 116 117 /** 118 * Sets the selected row position with smooth animation. 119 */ setSelectedPosition(int position)120 public void setSelectedPosition(int position) { 121 setSelectedPosition(position, true); 122 } 123 124 /** 125 * Sets the selected row position. 126 */ setSelectedPosition(int position, boolean smooth)127 public void setSelectedPosition(int position, boolean smooth) { 128 mSelectedPosition = position; 129 if(mVerticalGridView != null && mVerticalGridView.getAdapter() != null) { 130 if (smooth) { 131 mVerticalGridView.setSelectedPositionSmooth(position); 132 } else { 133 mVerticalGridView.setSelectedPosition(position); 134 } 135 } 136 } 137 getVerticalGridView()138 final VerticalGridView getVerticalGridView() { 139 return mVerticalGridView; 140 } 141 updateAdapter()142 void updateAdapter() { 143 mBridgeAdapter = null; 144 145 if (mAdapter != null) { 146 // If presenter selector is null, adapter ps will be used 147 mBridgeAdapter = new ItemBridgeAdapter(mAdapter, mPresenterSelector); 148 } 149 if (mVerticalGridView != null) { 150 mVerticalGridView.setAdapter(mBridgeAdapter); 151 if (mBridgeAdapter != null && mSelectedPosition != -1) { 152 mVerticalGridView.setSelectedPosition(mSelectedPosition); 153 } 154 } 155 } 156 getItem(Row row, int position)157 Object getItem(Row row, int position) { 158 if (row instanceof ListRow) { 159 return ((ListRow) row).getAdapter().get(position); 160 } else { 161 return null; 162 } 163 } 164 onTransitionStart()165 void onTransitionStart() { 166 if (mVerticalGridView != null) { 167 mVerticalGridView.setAnimateChildLayout(false); 168 mVerticalGridView.setPruneChild(false); 169 mVerticalGridView.setFocusSearchDisabled(true); 170 } 171 } 172 onTransitionEnd()173 void onTransitionEnd() { 174 if (mVerticalGridView != null) { 175 mVerticalGridView.setAnimateChildLayout(true); 176 mVerticalGridView.setPruneChild(true); 177 mVerticalGridView.setFocusSearchDisabled(false); 178 } 179 } 180 setItemAlignment()181 void setItemAlignment() { 182 if (mVerticalGridView != null) { 183 // align the top edge of item 184 mVerticalGridView.setItemAlignmentOffset(0); 185 mVerticalGridView.setItemAlignmentOffsetPercent( 186 VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED); 187 } 188 } 189 setWindowAlignmentFromTop(int alignedTop)190 void setWindowAlignmentFromTop(int alignedTop) { 191 if (mVerticalGridView != null) { 192 // align to a fixed position from top 193 mVerticalGridView.setWindowAlignmentOffset(alignedTop); 194 mVerticalGridView.setWindowAlignmentOffsetPercent( 195 VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED); 196 mVerticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE); 197 } 198 } 199 } 200