Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 16 | package com.android.launcher3.allapps; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 17 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 18 | import android.content.Context; |
Winson Chung | 243fdd7 | 2015-06-22 19:48:07 -0700 | [diff] [blame] | 19 | import android.graphics.Canvas; |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Winson Chung | 24cf700 | 2015-03-30 14:25:04 -0700 | [diff] [blame] | 21 | import android.support.v7.widget.LinearLayoutManager; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 22 | import android.support.v7.widget.RecyclerView; |
| 23 | import android.util.AttributeSet; |
Winson Chung | f819dc2 | 2015-03-23 14:45:54 -0700 | [diff] [blame] | 24 | import android.view.View; |
Sunny Goyal | b713ad4 | 2015-06-24 11:45:32 -0700 | [diff] [blame] | 25 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 26 | import com.android.launcher3.BaseRecyclerView; |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 27 | import com.android.launcher3.BaseRecyclerViewFastScrollBar; |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 28 | import com.android.launcher3.DeviceProfile; |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 29 | import com.android.launcher3.Stats; |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 30 | import com.android.launcher3.Utilities; |
Sunny Goyal | b713ad4 | 2015-06-24 11:45:32 -0700 | [diff] [blame] | 31 | import com.android.launcher3.util.Thunk; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 36 | * A RecyclerView with custom fast scroll support for the all apps view. |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 37 | */ |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 38 | public class AllAppsRecyclerView extends BaseRecyclerView |
| 39 | implements Stats.LaunchSourceProvider { |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 40 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 41 | private static final int FAST_SCROLL_MODE_JUMP_TO_FIRST_ICON = 0; |
| 42 | private static final int FAST_SCROLL_MODE_FREE_SCROLL = 1; |
| 43 | |
| 44 | private static final int FAST_SCROLL_BAR_MODE_DISTRIBUTE_BY_ROW = 0; |
| 45 | private static final int FAST_SCROLL_BAR_MODE_DISTRIBUTE_BY_SECTIONS = 1; |
| 46 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 47 | private AlphabeticalAppsList mApps; |
| 48 | private int mNumAppsPerRow; |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 49 | |
Sunny Goyal | b713ad4 | 2015-06-24 11:45:32 -0700 | [diff] [blame] | 50 | @Thunk BaseRecyclerViewFastScrollBar.FastScrollFocusableView mLastFastScrollFocusedView; |
| 51 | @Thunk int mPrevFastScrollFocusedPosition; |
| 52 | @Thunk int mFastScrollFrameIndex; |
| 53 | @Thunk final int[] mFastScrollFrames = new int[10]; |
| 54 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 55 | private final int mFastScrollMode = FAST_SCROLL_MODE_JUMP_TO_FIRST_ICON; |
| 56 | private final int mScrollBarMode = FAST_SCROLL_BAR_MODE_DISTRIBUTE_BY_ROW; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 57 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 58 | private ScrollPositionState mScrollPosState = new ScrollPositionState(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 59 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 60 | public AllAppsRecyclerView(Context context) { |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 61 | this(context, null); |
| 62 | } |
| 63 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 64 | public AllAppsRecyclerView(Context context, AttributeSet attrs) { |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 65 | this(context, attrs, 0); |
| 66 | } |
| 67 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 68 | public AllAppsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 69 | this(context, attrs, defStyleAttr, 0); |
| 70 | } |
| 71 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 72 | public AllAppsRecyclerView(Context context, AttributeSet attrs, int defStyleAttr, |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 73 | int defStyleRes) { |
| 74 | super(context, attrs, defStyleAttr); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sets the list of apps in this view, used to determine the fastscroll position. |
| 79 | */ |
| 80 | public void setApps(AlphabeticalAppsList apps) { |
| 81 | mApps = apps; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Sets the number of apps per row in this recycler view. |
| 86 | */ |
Winson Chung | 1cddad6 | 2015-06-25 16:46:47 -0700 | [diff] [blame] | 87 | public void setNumAppsPerRow(DeviceProfile grid, int numAppsPerRow) { |
Winson Chung | 208ed75 | 2015-05-12 19:05:30 -0700 | [diff] [blame] | 88 | mNumAppsPerRow = numAppsPerRow; |
Winson Chung | da25da3 | 2015-05-15 16:30:08 -0700 | [diff] [blame] | 89 | |
Winson Chung | da25da3 | 2015-05-15 16:30:08 -0700 | [diff] [blame] | 90 | RecyclerView.RecycledViewPool pool = getRecycledViewPool(); |
| 91 | int approxRows = (int) Math.ceil(grid.availableHeightPx / grid.allAppsIconSizePx); |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 92 | pool.setMaxRecycledViews(AllAppsGridAdapter.EMPTY_SEARCH_VIEW_TYPE, 1); |
| 93 | pool.setMaxRecycledViews(AllAppsGridAdapter.ICON_VIEW_TYPE, approxRows * mNumAppsPerRow); |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 94 | pool.setMaxRecycledViews(AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE, mNumAppsPerRow); |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 95 | pool.setMaxRecycledViews(AllAppsGridAdapter.SECTION_BREAK_VIEW_TYPE, approxRows); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Winson Chung | ed0c1cc | 2015-05-12 13:01:54 -0700 | [diff] [blame] | 99 | * Scrolls this recycler view to the top. |
| 100 | */ |
| 101 | public void scrollToTop() { |
| 102 | scrollToPosition(0); |
Winson Chung | 32f1407 | 2015-05-15 12:03:37 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Winson Chung | 243fdd7 | 2015-06-22 19:48:07 -0700 | [diff] [blame] | 106 | * We need to override the draw to ensure that we don't draw the overscroll effect beyond the |
| 107 | * background bounds. |
| 108 | */ |
| 109 | @Override |
| 110 | protected void dispatchDraw(Canvas canvas) { |
| 111 | canvas.clipRect(mBackgroundPadding.left, mBackgroundPadding.top, |
| 112 | getWidth() - mBackgroundPadding.right, |
| 113 | getHeight() - mBackgroundPadding.bottom); |
| 114 | super.dispatchDraw(canvas); |
| 115 | } |
| 116 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 117 | @Override |
| 118 | protected void onFinishInflate() { |
Hyunyoung Song | ebe1734 | 2015-05-06 17:49:33 -0700 | [diff] [blame] | 119 | super.onFinishInflate(); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 120 | |
| 121 | // Bind event handlers |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 122 | addOnItemTouchListener(this); |
| 123 | } |
| 124 | |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 125 | @Override |
| 126 | public void fillInLaunchSourceData(Bundle sourceData) { |
| 127 | sourceData.putString(Stats.SOURCE_EXTRA_CONTAINER, Stats.CONTAINER_ALL_APPS); |
| 128 | if (mApps.hasFilter()) { |
| 129 | sourceData.putString(Stats.SOURCE_EXTRA_SUB_CONTAINER, |
| 130 | Stats.SUB_CONTAINER_ALL_APPS_SEARCH); |
| 131 | } else { |
| 132 | sourceData.putString(Stats.SOURCE_EXTRA_SUB_CONTAINER, |
| 133 | Stats.SUB_CONTAINER_ALL_APPS_A_Z); |
| 134 | } |
| 135 | } |
| 136 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 137 | /** |
Winson Chung | 99d96ba | 2015-05-08 12:04:45 -0700 | [diff] [blame] | 138 | * Maps the touch (from 0..1) to the adapter position that should be visible. |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 139 | */ |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public String scrollToPositionAtProgress(float touchFraction) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 142 | int rowCount = mApps.getNumAppRows(); |
| 143 | if (rowCount == 0) { |
Winson Chung | 9121fbf | 2015-04-06 15:12:49 -0700 | [diff] [blame] | 144 | return ""; |
| 145 | } |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 146 | |
Winson Chung | 208ed75 | 2015-05-12 19:05:30 -0700 | [diff] [blame] | 147 | // Stop the scroller if it is scrolling |
Winson Chung | 208ed75 | 2015-05-12 19:05:30 -0700 | [diff] [blame] | 148 | stopScroll(); |
| 149 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 150 | // Find the fastscroll section that maps to this touch fraction |
| 151 | List<AlphabeticalAppsList.FastScrollSectionInfo> fastScrollSections = |
| 152 | mApps.getFastScrollerSections(); |
| 153 | AlphabeticalAppsList.FastScrollSectionInfo lastInfo = fastScrollSections.get(0); |
| 154 | if (mScrollBarMode == FAST_SCROLL_BAR_MODE_DISTRIBUTE_BY_ROW) { |
| 155 | for (int i = 1; i < fastScrollSections.size(); i++) { |
| 156 | AlphabeticalAppsList.FastScrollSectionInfo info = fastScrollSections.get(i); |
| 157 | if (info.touchFraction > touchFraction) { |
| 158 | break; |
| 159 | } |
| 160 | lastInfo = info; |
| 161 | } |
| 162 | } else if (mScrollBarMode == FAST_SCROLL_BAR_MODE_DISTRIBUTE_BY_SECTIONS){ |
| 163 | lastInfo = fastScrollSections.get((int) (touchFraction * (fastScrollSections.size() - 1))); |
| 164 | } else { |
| 165 | throw new RuntimeException("Unexpected scroll bar mode"); |
| 166 | } |
| 167 | |
| 168 | // Map the touch position back to the scroll of the recycler view |
| 169 | getCurScrollState(mScrollPosState, mApps.getAdapterItems()); |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 170 | int availableScrollHeight = getAvailableScrollHeight(rowCount, mScrollPosState.rowHeight, 0); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 171 | LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager(); |
| 172 | if (mFastScrollMode == FAST_SCROLL_MODE_FREE_SCROLL) { |
| 173 | layoutManager.scrollToPositionWithOffset(0, (int) -(availableScrollHeight * touchFraction)); |
| 174 | } |
| 175 | |
| 176 | if (mPrevFastScrollFocusedPosition != lastInfo.fastScrollToItem.position) { |
| 177 | mPrevFastScrollFocusedPosition = lastInfo.fastScrollToItem.position; |
| 178 | |
| 179 | // Reset the last focused view |
| 180 | if (mLastFastScrollFocusedView != null) { |
| 181 | mLastFastScrollFocusedView.setFastScrollFocused(false, true); |
| 182 | mLastFastScrollFocusedView = null; |
| 183 | } |
| 184 | |
| 185 | if (mFastScrollMode == FAST_SCROLL_MODE_JUMP_TO_FIRST_ICON) { |
| 186 | smoothSnapToPosition(mPrevFastScrollFocusedPosition, mScrollPosState); |
| 187 | } else if (mFastScrollMode == FAST_SCROLL_MODE_FREE_SCROLL) { |
| 188 | final ViewHolder vh = findViewHolderForPosition(mPrevFastScrollFocusedPosition); |
| 189 | if (vh != null && |
| 190 | vh.itemView instanceof BaseRecyclerViewFastScrollBar.FastScrollFocusableView) { |
| 191 | mLastFastScrollFocusedView = |
| 192 | (BaseRecyclerViewFastScrollBar.FastScrollFocusableView) vh.itemView; |
| 193 | mLastFastScrollFocusedView.setFastScrollFocused(true, true); |
| 194 | } |
| 195 | } else { |
| 196 | throw new RuntimeException("Unexpected fast scroll mode"); |
Winson Chung | 208ed75 | 2015-05-12 19:05:30 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 199 | return lastInfo.sectionName; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 200 | } |
Winson Chung | f819dc2 | 2015-03-23 14:45:54 -0700 | [diff] [blame] | 201 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 202 | @Override |
| 203 | public void onFastScrollCompleted() { |
| 204 | super.onFastScrollCompleted(); |
| 205 | // Reset and clean up the last focused view |
| 206 | if (mLastFastScrollFocusedView != null) { |
| 207 | mLastFastScrollFocusedView.setFastScrollFocused(false, true); |
| 208 | mLastFastScrollFocusedView = null; |
Winson Chung | f819dc2 | 2015-03-23 14:45:54 -0700 | [diff] [blame] | 209 | } |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 210 | mPrevFastScrollFocusedPosition = -1; |
Winson Chung | f819dc2 | 2015-03-23 14:45:54 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 214 | * Updates the bounds for the scrollbar. |
| 215 | */ |
| 216 | @Override |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 217 | public void onUpdateScrollbar() { |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 218 | List<AlphabeticalAppsList.AdapterItem> items = mApps.getAdapterItems(); |
| 219 | |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 220 | // Skip early if there are no items or we haven't been measured |
| 221 | if (items.isEmpty() || mNumAppsPerRow == 0) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 222 | mScrollbar.setScrollbarThumbOffset(-1, -1); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Find the index and height of the first visible row (all rows have the same height) |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 227 | int rowCount = mApps.getNumAppRows(); |
| 228 | getCurScrollState(mScrollPosState, items); |
| 229 | if (mScrollPosState.rowIndex < 0) { |
| 230 | mScrollbar.setScrollbarThumbOffset(-1, -1); |
| 231 | return; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 232 | } |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 233 | |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 234 | synchronizeScrollBarThumbOffsetToViewScroll(mScrollPosState, rowCount, 0); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Winson Chung | ed0c1cc | 2015-05-12 13:01:54 -0700 | [diff] [blame] | 237 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 238 | * This runnable runs a single frame of the smooth scroll animation and posts the next frame |
| 239 | * if necessary. |
| 240 | */ |
Sunny Goyal | b713ad4 | 2015-06-24 11:45:32 -0700 | [diff] [blame] | 241 | @Thunk Runnable mSmoothSnapNextFrameRunnable = new Runnable() { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 242 | @Override |
| 243 | public void run() { |
| 244 | if (mFastScrollFrameIndex < mFastScrollFrames.length) { |
| 245 | scrollBy(0, mFastScrollFrames[mFastScrollFrameIndex]); |
| 246 | mFastScrollFrameIndex++; |
| 247 | postOnAnimation(mSmoothSnapNextFrameRunnable); |
| 248 | } else { |
| 249 | // Animation completed, set the fast scroll state on the target view |
| 250 | final ViewHolder vh = findViewHolderForPosition(mPrevFastScrollFocusedPosition); |
| 251 | if (vh != null && |
| 252 | vh.itemView instanceof BaseRecyclerViewFastScrollBar.FastScrollFocusableView && |
| 253 | mLastFastScrollFocusedView != vh.itemView) { |
| 254 | mLastFastScrollFocusedView = |
| 255 | (BaseRecyclerViewFastScrollBar.FastScrollFocusableView) vh.itemView; |
| 256 | mLastFastScrollFocusedView.setFastScrollFocused(true, true); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | /** |
| 263 | * Smoothly snaps to a given position. We do this manually by calculating the keyframes |
| 264 | * ourselves and animating the scroll on the recycler view. |
| 265 | */ |
| 266 | private void smoothSnapToPosition(final int position, ScrollPositionState scrollPosState) { |
| 267 | removeCallbacks(mSmoothSnapNextFrameRunnable); |
| 268 | |
| 269 | // Calculate the full animation from the current scroll position to the final scroll |
| 270 | // position, and then run the animation for the duration. |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 271 | int curScrollY = getPaddingTop() + |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 272 | (scrollPosState.rowIndex * scrollPosState.rowHeight) - scrollPosState.rowTopOffset; |
| 273 | int newScrollY = getScrollAtPosition(position, scrollPosState.rowHeight); |
| 274 | int numFrames = mFastScrollFrames.length; |
| 275 | for (int i = 0; i < numFrames; i++) { |
| 276 | // TODO(winsonc): We can interpolate this as well. |
| 277 | mFastScrollFrames[i] = (newScrollY - curScrollY) / numFrames; |
| 278 | } |
| 279 | mFastScrollFrameIndex = 0; |
| 280 | postOnAnimation(mSmoothSnapNextFrameRunnable); |
| 281 | } |
| 282 | |
| 283 | /** |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 284 | * Returns the current scroll state of the apps rows. |
Winson Chung | ed0c1cc | 2015-05-12 13:01:54 -0700 | [diff] [blame] | 285 | */ |
| 286 | private void getCurScrollState(ScrollPositionState stateOut, |
| 287 | List<AlphabeticalAppsList.AdapterItem> items) { |
Winson Chung | ed0c1cc | 2015-05-12 13:01:54 -0700 | [diff] [blame] | 288 | stateOut.rowIndex = -1; |
| 289 | stateOut.rowTopOffset = -1; |
| 290 | stateOut.rowHeight = -1; |
Winson Chung | 532e6ea | 2015-05-14 12:24:25 -0700 | [diff] [blame] | 291 | |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 292 | // Return early if there are no items or we haven't been measured |
| 293 | if (items.isEmpty() || mNumAppsPerRow == 0) { |
Winson Chung | 532e6ea | 2015-05-14 12:24:25 -0700 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Winson Chung | ed0c1cc | 2015-05-12 13:01:54 -0700 | [diff] [blame] | 297 | int childCount = getChildCount(); |
| 298 | for (int i = 0; i < childCount; i++) { |
| 299 | View child = getChildAt(i); |
| 300 | int position = getChildPosition(child); |
| 301 | if (position != NO_POSITION) { |
| 302 | AlphabeticalAppsList.AdapterItem item = items.get(position); |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 303 | if (item.viewType == AllAppsGridAdapter.ICON_VIEW_TYPE || |
| 304 | item.viewType == AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 305 | stateOut.rowIndex = item.rowIndex; |
Winson Chung | ed0c1cc | 2015-05-12 13:01:54 -0700 | [diff] [blame] | 306 | stateOut.rowTopOffset = getLayoutManager().getDecoratedTop(child); |
| 307 | stateOut.rowHeight = child.getHeight(); |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | * Returns the scrollY for the given position in the adapter. |
| 316 | */ |
| 317 | private int getScrollAtPosition(int position, int rowHeight) { |
| 318 | AlphabeticalAppsList.AdapterItem item = mApps.getAdapterItems().get(position); |
Winson Chung | 1ae7a50 | 2015-07-06 17:14:51 -0700 | [diff] [blame] | 319 | if (item.viewType == AllAppsGridAdapter.ICON_VIEW_TYPE || |
| 320 | item.viewType == AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE) { |
| 321 | int offset = item.rowIndex > 0 ? getPaddingTop() : 0; |
| 322 | return offset + item.rowIndex * rowHeight; |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 323 | } else { |
| 324 | return 0; |
| 325 | } |
| 326 | } |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 327 | } |