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 */ 16 package com.android.launcher3.allapps; 17 18 import androidx.recyclerview.widget.LinearSmoothScroller; 19 import androidx.recyclerview.widget.RecyclerView.ViewHolder; 20 21 import com.android.launcher3.allapps.AlphabeticalAppsList.FastScrollSectionInfo; 22 23 public class AllAppsFastScrollHelper { 24 25 private static final int NO_POSITION = -1; 26 27 private int mTargetFastScrollPosition = NO_POSITION; 28 29 private AllAppsRecyclerView mRv; 30 private ViewHolder mLastSelectedViewHolder; 31 AllAppsFastScrollHelper(AllAppsRecyclerView rv)32 public AllAppsFastScrollHelper(AllAppsRecyclerView rv) { 33 mRv = rv; 34 } 35 36 /** 37 * Smooth scrolls the recycler view to the given section. 38 */ smoothScrollToSection(FastScrollSectionInfo info)39 public void smoothScrollToSection(FastScrollSectionInfo info) { 40 if (mTargetFastScrollPosition == info.fastScrollToItem.position) { 41 return; 42 } 43 mTargetFastScrollPosition = info.fastScrollToItem.position; 44 mRv.getLayoutManager().startSmoothScroll(new MyScroller(mTargetFastScrollPosition)); 45 } 46 onFastScrollCompleted()47 public void onFastScrollCompleted() { 48 mTargetFastScrollPosition = NO_POSITION; 49 setLastHolderSelected(false); 50 mLastSelectedViewHolder = null; 51 } 52 53 setLastHolderSelected(boolean isSelected)54 private void setLastHolderSelected(boolean isSelected) { 55 if (mLastSelectedViewHolder != null) { 56 mLastSelectedViewHolder.itemView.setActivated(isSelected); 57 mLastSelectedViewHolder.setIsRecyclable(!isSelected); 58 } 59 } 60 61 private class MyScroller extends LinearSmoothScroller { 62 63 private final int mTargetPosition; 64 MyScroller(int targetPosition)65 public MyScroller(int targetPosition) { 66 super(mRv.getContext()); 67 68 mTargetPosition = targetPosition; 69 setTargetPosition(targetPosition); 70 } 71 72 @Override getVerticalSnapPreference()73 protected int getVerticalSnapPreference() { 74 return SNAP_TO_START; 75 } 76 77 @Override onStop()78 protected void onStop() { 79 super.onStop(); 80 if (mTargetPosition != mTargetFastScrollPosition) { 81 // Target changed, before the last scroll can finish 82 return; 83 } 84 85 ViewHolder currentHolder = mRv.findViewHolderForAdapterPosition(mTargetPosition); 86 if (currentHolder == mLastSelectedViewHolder) { 87 return; 88 } 89 90 setLastHolderSelected(false); 91 mLastSelectedViewHolder = currentHolder; 92 setLastHolderSelected(true); 93 } 94 95 @Override onStart()96 protected void onStart() { 97 super.onStart(); 98 if (mTargetPosition != mTargetFastScrollPosition) { 99 setLastHolderSelected(false); 100 mLastSelectedViewHolder = null; 101 } 102 } 103 } 104 } 105